Skip to content
Permalink
Browse files

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

  • Loading branch information...
dereuromark committed Jun 16, 2015
1 parent 65b64e0 commit ccac3b3e06fbb9bb7aa95ad912e0647e5c0682a7
Showing with 47 additions and 1 deletion.
  1. +28 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +19 −1 lib/Cake/View/Helper/FormHelper.php
@@ -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
*
@@ -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
@@ -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';
@@ -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

0 comments on commit ccac3b3

Please sign in to comment.
You can’t perform that action at this time.