Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Unable to globally set disabled attribute on radio buttons #14565

Copy link
Copy link
Closed
@webdevilopers

Description

@webdevilopers
Issue body actions

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.