Disable radio button not working #4136

Closed
Chintanvpatel opened this Issue Mar 28, 2013 · 2 comments

Comments

Projects
None yet
2 participants
@Chintanvpatel
Contributor

Chintanvpatel commented Mar 28, 2013

Adding form element in form using
$this->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender',
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
)
));

On view
echo $this->formRow($form->get('gender'));

When i try to disable it from controller using below code, It's not working.
$form->get('gender')->setAttribute('disabled', 'true');

@grizzm0

This comment has been minimized.

Show comment
Hide comment
@grizzm0

grizzm0 Mar 28, 2013

Contributor

I don't have time to debug it and create a PR atm. But atleast I tracked it down to these rows.

$options simply contain a key => value pair of the value_options key.

array(2) {
  [0] => string(6) "Female"
  [1] => string(4) "Male"
}

Therefore the isset($optionSpec['disabled']) will always return false and $disabled will never be set.
This seems to do the trick.

            if (is_scalar($optionSpec)) {
                $optionSpec = array(
                    'label' => $optionSpec,
                    'value' => $key
                );

                if (isset($inputAttributes['disabled'])) {
                    $disabled = $inputAttributes['disabled'];
                }
            }
Contributor

grizzm0 commented Mar 28, 2013

I don't have time to debug it and create a PR atm. But atleast I tracked it down to these rows.

$options simply contain a key => value pair of the value_options key.

array(2) {
  [0] => string(6) "Female"
  [1] => string(4) "Male"
}

Therefore the isset($optionSpec['disabled']) will always return false and $disabled will never be set.
This seems to do the trick.

            if (is_scalar($optionSpec)) {
                $optionSpec = array(
                    'label' => $optionSpec,
                    'value' => $key
                );

                if (isset($inputAttributes['disabled'])) {
                    $disabled = $inputAttributes['disabled'];
                }
            }
@grizzm0

This comment has been minimized.

Show comment
Hide comment
@grizzm0

grizzm0 Mar 28, 2013

Contributor

Oh, I see what happens. :o $disabled is set to false at the top of the foreach, it then checks to see if $optionsSpec['diabled'] is set. It then sets $inputAttributes['disabled'] = $disabled which is false if $optionsSpec['disabled'] isn't set. I'm not sure who coded this and how it's intended to work. But the code below works.

            $value           = '';
            $label           = '';
            $selected        = false;
            $disabled        = isset($attributes['disabled']) ? true : false;
            $inputAttributes = $attributes;
            $labelAttributes = $globalLabelAttributes;
Contributor

grizzm0 commented Mar 28, 2013

Oh, I see what happens. :o $disabled is set to false at the top of the foreach, it then checks to see if $optionsSpec['diabled'] is set. It then sets $inputAttributes['disabled'] = $disabled which is false if $optionsSpec['disabled'] isn't set. I'm not sure who coded this and how it's intended to work. But the code below works.

            $value           = '';
            $label           = '';
            $selected        = false;
            $disabled        = isset($attributes['disabled']) ? true : false;
            $inputAttributes = $attributes;
            $labelAttributes = $globalLabelAttributes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment