Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Form group with radio is causing all radio buttons having checked attribute #20

Closed
therajumandapati opened this issue Oct 13, 2020 · 9 comments

Comments

@therajumandapati
Copy link

therajumandapati commented Oct 13, 2020

We're using <x-form-group> with a couple of <x-form-radio> elements inside. When a value is selected and the form reloads due to validation errors, all radio elements have the checked='checked' attribute.

image

I think this is due to the fact that <x-form-radio> component is using a class which extends FormCheckbox and because of the presence of this line: https://github.com/protonemedia/laravel-form-components/blob/master/src/Components/FormCheckbox.php#L33

It could be fixed by changing FormRadio.php to the following:

<?php

namespace ProtoneMedia\LaravelFormComponents\Components;

class FormRadio extends Component
{
    use HandlesValidationErrors;
    use HandlesBoundValues;

    public string $name;
    public string $label;
    public $value;
    public bool $checked = false;

    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(
        string $name,
        string $label = '',
        $value = 1,
        $bind = null,
        bool $default = false,
        bool $showErrors = true
    ) {
        $this->name       = $name;
        $this->label      = $label;
        $this->value      = $value;
        $this->showErrors = $showErrors;

        if (old($name) == $value) { <== CHANGE
            $this->checked = true;
        }

        if (!session()->hasOldInput() && $this->isNotWired()) {
            $boundValue = $this->getBoundValue($bind, $name);

            $this->checked = is_null($boundValue) ? $default : $boundValue == $value; <== CHANGE
        }
    }
}
@pascalbaljet
Copy link
Contributor

Thans for reporting, I'll try to fix it this week.

@betaselect
Copy link

Thanks @therajumandapati, you saved me quite some time!

pascalbaljet added a commit that referenced this issue Oct 16, 2020
@pascalbaljet
Copy link
Contributor

pascalbaljet commented Oct 16, 2020

Could you both try this branch and confirm it has been fixed? Thanks!

composer require protonemedia/laravel-form-components:dev-radio-bugfix-20

@pascalbaljet
Copy link
Contributor

@Inwave @therajumandapati any news on testing the bugfix?

@betaselect
Copy link

Will check now!

@betaselect
Copy link

Just tested it and it works on my end. Thanks @pascalbaljet

@pascalbaljet
Copy link
Contributor

Thanks, I'll merge it soon!

pascalbaljet added a commit that referenced this issue Oct 21, 2020
@pascalbaljet
Copy link
Contributor

Fixed in v2.1.2

@therajumandapati
Copy link
Author

Thanks for the fast turn around @pascalbaljet

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants