Allow deep options for radio() just as for select().

This commit is contained in:
Mark Scherer 2015-06-16 13:23:51 +02:00
parent 65b64e0348
commit ccac3b3e06
2 changed files with 47 additions and 1 deletions

View file

@ -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
*

View file

@ -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