Applying patch from 'NaMB' to add disabled support to FormHelper::radio(). Test cases added as well. Fixes #1459

This commit is contained in:
mark_story 2011-03-13 19:35:37 -04:00
parent 30a9543a65
commit b9ec7da21d
2 changed files with 38 additions and 0 deletions

View file

@ -1039,6 +1039,7 @@ class FormHelper extends AppHelper {
public function radio($fieldName, $options = array(), $attributes = array()) {
$attributes = $this->_initInputField($fieldName, $attributes);
$legend = false;
$disabled = array();
if (isset($attributes['legend'])) {
$legend = $attributes['legend'];
@ -1064,6 +1065,11 @@ class FormHelper extends AppHelper {
} else {
$value = $this->value($fieldName);
}
if (isset($attributes['disabled'])) {
$disabled = $attributes['disabled'];
}
$out = array();
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
@ -1075,6 +1081,9 @@ class FormHelper extends AppHelper {
if (isset($value) && $optValue == $value) {
$optionsHere['checked'] = 'checked';
}
if (!empty($disabled) && in_array($optValue, $disabled)) {
$optionsHere['disabled'] = true;
}
$tagName = Inflector::camelize(
$attributes['id'] . '_' . Inflector::slug($optValue)
);

View file

@ -3012,6 +3012,35 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
}
/**
* test disabled radio options
*
* @return void
*/
function testRadioDisabled() {
$result = $this->Form->radio(
'Model.field',
array('option A', 'option B'),
array('disabled' => array('option A'), 'value' => 'option A')
);
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
array('label' => array('for' => 'ModelField0')),
'option A',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'option B',
'/label',
'/fieldset'
);
$this->assertTags($result, $expected);
}
/**
* test disabling the hidden input for radio buttons
*