diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index a1ef2cd9e..bbd81dd7c 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -4432,6 +4432,34 @@ class FormHelperTest extends CakeTestCase { $this->assertTextNotContains('"Model1Field"', $result); } +/** + * Test that radio() accepts a deep array for options + * + * @return void + */ + public function testRadioOptionsArray() { + $result = $this->Form->input('Model.field', array( + 'type' => 'radio', + 'legend' => false, + 'div' => false, + 'options' => array( + '1' => array('name' => 'Option A', 'title' => 'A Title'), + '2' => array('name' => 'Option B', 'data-foo' => 'bar')) + )); + $expected = array( + array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')), + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'value' => '1', 'title' => 'A Title')), + array('label' => array('for' => 'ModelField1')), + 'Option A', + '/label', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2', 'data-foo' => 'bar')), + array('label' => array('for' => 'ModelField2')), + 'Option B', + '/label' + ); + $this->assertTags($result, $expected); + } + /** * Test that radio() accepts an array for label * diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 2a9f5be6c..1edbe7551 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1471,6 +1471,15 @@ class FormHelper extends AppHelper { * Creates a set of radio widgets. Will create a legend and fieldset * by default. Use $options to control this * + * You can also customize each radio input element using an array of arrays: + * + * ``` + * $options = array( + * array('name' => 'United states', 'value' => 'US', 'title' => 'My title'), + * array('name' => 'Germany', 'value' => 'DE', 'class' => 'de-de', 'title' => 'Another title'), + * ); + * ``` + * * ### Attributes: * * - `separator` - define the string in between the radio buttons @@ -1553,6 +1562,15 @@ class FormHelper extends AppHelper { $this->_domIdSuffixes = array(); foreach ($options as $optValue => $optTitle) { $optionsHere = array('value' => $optValue, 'disabled' => false); + if (is_array($optTitle)) { + if (isset($optTitle['value'])) { + $optionsHere['value'] = $optTitle['value']; + } + + $optionsHere += $optTitle; + $optTitle = $optionsHere['name']; + unset($optionsHere['name']); + } if (isset($value) && strval($optValue) === strval($value)) { $optionsHere['checked'] = 'checked'; @@ -1572,7 +1590,7 @@ class FormHelper extends AppHelper { if (is_array($between)) { $optTitle .= array_shift($between); } - $allOptions = array_merge($attributes, $optionsHere); + $allOptions = $optionsHere + $attributes; $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName, array_diff_key($allOptions, array('name' => null, 'type' => null, 'id' => null)), $optTitle