Skip to content

Commit

Permalink
Make keys consistent with attributes.
Browse files Browse the repository at this point in the history
Having the value attribute use the name key is silly. It also makes life
hard when trying to make radio + select boxes use the same data format.
Use 'value' and 'text' instead, as they match the attributes/DOM names
and don't conflict in the case of radio buttons.
  • Loading branch information
markstory committed Jan 9, 2014
1 parent bd448bc commit 351929d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
22 changes: 11 additions & 11 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -73,11 +73,11 @@ public function __construct($templates) {
*
* {{{
* 'options' => [
* ['name' => 'elk', 'value' => 'Elk', 'data-foo' => 'bar'],
* ['value' => 'elk', 'text' => 'Elk', 'data-foo' => 'bar'],
* ]
* }}}
*
* This form **requires** that both the `name` and `value` keys be defined.
* This form **requires** that both the `value` and `text` keys be defined.
* If either is not set options will not be generated correctly.
*
* If you need to define option groups you can do those using nested arrays:
Expand All @@ -97,7 +97,7 @@ public function __construct($templates) {
* {{{
* 'options' => [
* [
* 'name' => 'Mammals',
* 'text' => 'Mammals',
* 'data-id' => 1,
* 'options' => [
* 'elk' => 'Elk',
Expand Down Expand Up @@ -184,17 +184,17 @@ protected function _renderContent($data) {
protected function _renderOptgroup($label, $optgroup, $disabled, $selected, $escape) {
$opts = $optgroup;
$attrs = [];
if (isset($optgroup['options'], $optgroup['name'])) {
if (isset($optgroup['options'], $optgroup['text'])) {
$opts = $optgroup['options'];
$label = $optgroup['name'];
$label = $optgroup['text'];
$attrs = $optgroup;
}
$groupOptions = $this->_renderOptions($opts, $disabled, $selected, $escape);

return $this->_templates->format('optgroup', [
'label' => $escape ? h($label) : $label,
'content' => implode('', $groupOptions),
'attrs' => $this->_templates->formatAttributes($attrs, ['name', 'options']),
'attrs' => $this->_templates->formatAttributes($attrs, ['text', 'options']),
]);
}

Expand Down Expand Up @@ -224,10 +224,10 @@ protected function _renderOptions($options, $disabled, $selected, $escape) {

// Basic options
$optAttrs = [
'name' => $key,
'value' => $val,
'value' => $key,
'text' => $val,
];
if (is_array($val) && isset($optAttrs['name'], $optAttrs['value'])) {
if (is_array($val) && isset($optAttrs['text'], $optAttrs['value'])) {
$optAttrs = $val;
}
if ($this->_isSelected($key, $selected)) {
Expand All @@ -239,9 +239,9 @@ protected function _renderOptions($options, $disabled, $selected, $escape) {
$optAttrs['escape'] = $escape;

$out[] = $this->_templates->format('option', [
'name' => $escape ? h($optAttrs['name']) : $optAttrs['name'],
'value' => $escape ? h($optAttrs['value']) : $optAttrs['value'],
'attrs' => $this->_templates->formatAttributes($optAttrs, ['name', 'value']),
'text' => $escape ? h($optAttrs['text']) : $optAttrs['text'],
'attrs' => $this->_templates->formatAttributes($optAttrs, ['text', 'value']),
]);
}
return $out;
Expand Down
10 changes: 5 additions & 5 deletions Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -28,7 +28,7 @@ public function setUp() {
$templates = [
'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
'option' => '<option value="{{name}}"{{attrs}}>{{value}}</option>',
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
];
$this->templates = new StringTemplate();
Expand Down Expand Up @@ -112,8 +112,8 @@ public function testRenderComplex() {
'id' => 'BirdName',
'name' => 'Birds[name]',
'options' => [
['name' => 'a', 'value' => 'Albatross'],
['name' => 'b', 'value' => 'Budgie', 'data-foo' => 'bar'],
['value' => 'a', 'text' => 'Albatross'],
['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
]
];
$result = $select->render($data);
Expand Down Expand Up @@ -210,7 +210,7 @@ public function testRenderMultipleSelected() {
$data = [
'multiple' => true,
'id' => 'BirdName',
'name' => 'Birds[name][]',
'name' => 'Birds[name]',
'value' => ['1', '2', 'burp'],
'options' => [
1 => 'one',
Expand Down Expand Up @@ -292,7 +292,7 @@ public function testRenderOptionGroupsWithAttributes() {
'name' => 'Birds[name]',
'options' => [
[
'name' => 'Mammal',
'text' => 'Mammal',
'data-foo' => 'bar',
'options' => [
'beaver' => 'Beaver',
Expand Down

0 comments on commit 351929d

Please sign in to comment.