Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Unable to globally set disabled attribute on radio buttons #14565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
webdevilopers opened this issue May 6, 2015 · 2 comments
Closed

Unable to globally set disabled attribute on radio buttons #14565

webdevilopers opened this issue May 6, 2015 · 2 comments
Labels

Comments

@webdevilopers
Copy link

Originally I wanted to display radio buttons without the possibility of changing them.
I tried using the read_only attribute on my field type first:

$builder->add('onCall', 'choice', array(
    'choices' => array(0 => 'no', 1 => 'yes'),
    'expanded' => true,
    'read_only' => true
));

As expected the readonly attribute gets added to the input:

<input id="timekeeping_entry_day_onCall_0" type="radio" value="0" required="required" readonly="readonly" name="timekeeping_entry_day[onCall]">

But in the end this does not have any effect since - as we all know by now - readonly is not supported for radio buttons:
http://www.w3.org/TR/WD-forms-970402#readonly
READONLY applies to INPUT elements of type TEXT or PASSWORD and to the TEXTAREA element.

So allowing the read_only option on an expanded choice does not seem to make sense anyway, correct?

The desired effect on the element should be to disable it. But setting disabled on the field type will ignore validation after form submit if the radio buttons have been enabled again by a javascript on client side.

So it looks like the only way to do it is to set the HTML attribute disabled on the field type.
This way it can be enabled again by the client and the submitted value would not be ignored.

My attempt for this:

$builder->add('onCall', 'choice', array(
    'choices' => array(0 => 'no', 1 => 'yes'),
    'expanded' => true,
    'attr' => array('disabled' => 'disabled')
));

Unfortunately the HTML result does not apply the disabled attribute to the input but to the div around it:

<div id="timekeeping_entry_day_onCall" disabled="disabled">
<input id="timekeeping_entry_day_onCall_0" type="radio" value="0" required="required" name="timekeeping_entry_day[onCall]">
<label class="required" for="timekeeping_entry_day_onCall_0">no</label>
<input id="timekeeping_entry_day_onCall_1" type="radio" value="1" required="required" name="timekeeping_entry_day[onCall]">
<label class="required" for="timekeeping_entry_day_onCall_1">yes</label>
</div>

I'm not using the Bootstrap theme.

Setting the attr seems to be the issue:

The div uses the widget_container_attributes block:
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L46

While the input uses a widget_attributes block:
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L93

Are the attr passed to the wrong block?

Original issue I started:

Possibly related:

@webdevilopers
Copy link
Author

I just recognized the duplicate #3836 linked by @stof .

There is a workaround by @ryancastle and @gagarine for this using a custom template:
#3836 (comment)

Meanwhile there have been a lot of issues related to this topic.

I think the final state is this improvement already merged by @webmozart and set for 2.7.
This will introduce choice_attr see #14050

$builder->add('attending', 'choice', array(
    'choices' => array(
        'Yes' => true,
        'No' => false,
        'Maybe' => null,
    ),
    'choices_as_values' => true,
    'choice_attr' => array(
        'Maybe' => array('class' => 'greyed-out'),
    ),
));

Is the choice_attr then the right place to set the disabled attribute (globally)? Or for option as commented by @webda2l .

@webdevilopers webdevilopers changed the title Unable to set disabled attribute on radio buttons Unable to globally set disabled attribute on radio buttons May 6, 2015
@jakzal jakzal added the Form label May 12, 2015
@HeahDude
Copy link
Contributor

Hi @webdevilopers, I think you're right, the recent ability to use choice_attr should perfectly fit that use case.

Should be closed as fixed by #14050.

@Tobion Tobion closed this as completed Feb 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants