From b9ec7da21df75be54ce58c48145d329c0ea51495 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 13 Mar 2011 19:35:37 -0400 Subject: [PATCH] Applying patch from 'NaMB' to add disabled support to FormHelper::radio(). Test cases added as well. Fixes #1459 --- cake/libs/view/helpers/form.php | 9 ++++++ .../cases/libs/view/helpers/form.test.php | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 7b0b29b93..00ec12351 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -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) ); diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 532476960..d8ccbf617 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -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 *