Skip to content

Commit

Permalink
Allow deep options for radio() just as for select().
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Jun 16, 2015
1 parent 65b64e0 commit ccac3b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4432,6 +4432,34 @@ public function testRadioAddEmptyOption() {
$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
*
Expand Down
20 changes: 19 additions & 1 deletion lib/Cake/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,15 @@ public function checkbox($fieldName, $options = array()) {
* 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
Expand Down Expand Up @@ -1553,6 +1562,15 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
$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';
Expand All @@ -1572,7 +1590,7 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
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
Expand Down

0 comments on commit ccac3b3

Please sign in to comment.