From 88a3ccad3a952188570e8c61902a5ecc1a279e3e Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 13 Jan 2014 09:31:15 -0500 Subject: [PATCH] Use 'val' instead of 'selected'. Selected does not map well onto input types like text or hidden. Using val lets us not shadow the value attribute, and also mirrors jQuery so the concept shouldn't be foreign to developers. --- src/View/Input/Checkbox.php | 9 +++++---- src/View/Input/Radio.php | 6 +++--- src/View/Input/SelectBox.php | 8 ++++---- tests/TestCase/View/Input/CheckboxTest.php | 6 +++--- tests/TestCase/View/Input/RadioTest.php | 2 +- tests/TestCase/View/Input/SelectBoxTest.php | 16 ++++++++-------- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/View/Input/Checkbox.php b/src/View/Input/Checkbox.php index ee05c57ff9a..becbb642ca4 100644 --- a/src/View/Input/Checkbox.php +++ b/src/View/Input/Checkbox.php @@ -42,7 +42,8 @@ public function __construct($templates) { * * - `name` - The name of the input. * - `value` - The value attribute. Defaults to '1'. - * - `checked` - Whether or not the checkbox should be checked. + * - `val` - The current value. If it matches `value` the checkbox will be checked. + * You can also use the 'checked' attribute to make the checkbox checked. * - `disabled` - Whether or not the checkbox should be disabled. * * Any other attributes passed in will be treated as HTML attributes. @@ -54,14 +55,14 @@ public function render($data) { $data += [ 'name' => '', 'value' => 1, - 'selected' => null, + 'val' => null, 'checked' => false, 'disabled' => false, ]; if ($this->_isChecked($data)) { $data['checked'] = true; } - unset($data['selected']); + unset($data['val']); $attrs = $this->_templates->formatAttributes( $data, @@ -85,7 +86,7 @@ protected function _isChecked($data) { if (!empty($data['checked'])) { return true; } - if ((string)$data['selected'] === (string)$data['value']) { + if ((string)$data['val'] === (string)$data['value']) { return true; } return false; diff --git a/src/View/Input/Radio.php b/src/View/Input/Radio.php index 82a564afe72..96bbd6f8889 100644 --- a/src/View/Input/Radio.php +++ b/src/View/Input/Radio.php @@ -61,7 +61,7 @@ public function __construct($templates) { * - `options` - An array of options. See below for more information. * - `disabled` - Either true or an array of inputs to disable. * When true, the select element will be disabled. - * - `selected` - A string of the option to mark as selected. + * - `val` - A string of the option to mark as selected. * - `label` - Either false to disable label generation, or * an array of attributes for all labels. * @@ -73,7 +73,7 @@ public function render($data) { 'name' => '', 'options' => [], 'disabled' => null, - 'selected' => null, + 'val' => null, 'escape' => true, 'label' => true, 'empty' => false, @@ -137,7 +137,7 @@ protected function _renderInput($val, $text, $data) { $radio['id'] = Inflector::slug($radio['name'] . '_' . $radio['value']); } - if (isset($data['selected']) && strval($data['selected']) === strval($radio['value'])) { + if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) { $radio['checked'] = true; } diff --git a/src/View/Input/SelectBox.php b/src/View/Input/SelectBox.php index ee0a096afe6..9b965199ca4 100644 --- a/src/View/Input/SelectBox.php +++ b/src/View/Input/SelectBox.php @@ -51,7 +51,7 @@ public function __construct($templates) { * - `options` - An array of options. * - `disabled` - Either true or an array of options to disable. * When true, the select element will be disabled. - * - `selected` - Either a string or an array of options to mark as selected. + * - `val` - Either a string or an array of options to mark as selected. * - `empty` - Set to true to add an empty option at the top of the * option elements. Set to a string to define the display value of the * empty option. @@ -121,7 +121,7 @@ public function render($data) { 'escape' => true, 'options' => [], 'disabled' => null, - 'selected' => null, + 'val' => null, ]; if (empty($data['name'])) { @@ -129,7 +129,7 @@ public function render($data) { } $options = $this->_renderContent($data); $name = $data['name']; - unset($data['name'], $data['options'], $data['empty'], $data['selected'], $data['escape']); + unset($data['name'], $data['options'], $data['empty'], $data['val'], $data['escape']); if (isset($data['disabled']) && is_array($data['disabled'])) { unset($data['disabled']); } @@ -168,7 +168,7 @@ protected function _renderContent($data) { return []; } - $selected = isset($data['selected']) ? $data['selected'] : null; + $selected = isset($data['val']) ? $data['val'] : null; $disabled = null; if (isset($data['disabled']) && is_array($data['disabled'])) { $disabled = $data['disabled']; diff --git a/tests/TestCase/View/Input/CheckboxTest.php b/tests/TestCase/View/Input/CheckboxTest.php index 7388a0c3b74..9b9c110f310 100644 --- a/tests/TestCase/View/Input/CheckboxTest.php +++ b/tests/TestCase/View/Input/CheckboxTest.php @@ -116,19 +116,19 @@ public function testRenderChecked() { $data = [ 'name' => 'Comment[spam]', 'value' => 1, - 'selected' => 1, + 'val' => 1, ]; $result = $checkbox->render($data); $this->assertTags($result, $expected); - $data['selected'] = '1'; + $data['val'] = '1'; $result = $checkbox->render($data); $this->assertTags($result, $expected); $data = [ 'name' => 'Comment[spam]', 'value' => 1, - 'selected' => '1x', + 'val' => '1x', ]; $result = $checkbox->render($data); $expected = [ diff --git a/tests/TestCase/View/Input/RadioTest.php b/tests/TestCase/View/Input/RadioTest.php index ad28ba753ed..fe3dde25b62 100644 --- a/tests/TestCase/View/Input/RadioTest.php +++ b/tests/TestCase/View/Input/RadioTest.php @@ -215,7 +215,7 @@ public function testRenderSelected() { $radio = new Radio($this->templates); $data = [ 'name' => 'Versions[ver]', - 'selected' => '1', + 'val' => '1', 'options' => [ 1 => 'one', '1x' => 'one x', diff --git a/tests/TestCase/View/Input/SelectBoxTest.php b/tests/TestCase/View/Input/SelectBoxTest.php index df8dcad8cf1..1c31c5adb66 100644 --- a/tests/TestCase/View/Input/SelectBoxTest.php +++ b/tests/TestCase/View/Input/SelectBoxTest.php @@ -141,7 +141,7 @@ public function testRenderSelected() { $data = [ 'id' => 'BirdName', 'name' => 'Birds[name]', - 'selected' => '1', + 'val' => '1', 'options' => [ 1 => 'one', '1x' => 'one x', @@ -160,7 +160,7 @@ public function testRenderSelected() { ]; $this->assertTags($result, $expected); - $data['selected'] = 2; + $data['val'] = 2; $result = $select->render($data); $expected = [ 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], @@ -211,7 +211,7 @@ public function testRenderMultipleSelected() { 'multiple' => true, 'id' => 'BirdName', 'name' => 'Birds[name]', - 'selected' => ['1', '2', 'burp'], + 'val' => ['1', '2', 'burp'], 'options' => [ 1 => 'one', '1x' => 'one x', @@ -373,7 +373,7 @@ public function testRenderOptionGroupsSelectedAndDisabled() { $select = new SelectBox($this->templates); $data = [ 'name' => 'Birds[name]', - 'selected' => ['1', '2', 'burp'], + 'val' => ['1', '2', 'burp'], 'disabled' => ['1x', '2x', 'nope'], 'options' => [ 'ones' => [ @@ -416,7 +416,7 @@ public function testRenderDisabled() { 'disabled' => true, 'name' => 'Birds[name]', 'options' => ['a' => 'Albatross', 'b' => 'Budgie'], - 'selected' => 'a', + 'val' => 'a', ]; $result = $select->render($data); $expected = [ @@ -440,7 +440,7 @@ public function testRenderDisabledMultiple() { $select = new SelectBox($this->templates); $data = [ 'disabled' => ['a', 'c'], - 'selected' => 'a', + 'val' => 'a', 'name' => 'Birds[name]', 'options' => [ 'a' => 'Albatross', @@ -502,7 +502,7 @@ public function testRenderEmptyOption() { $this->assertTags($result, $expected); $data['empty'] = 'empty'; - $data['selected'] = ''; + $data['val'] = ''; $result = $select->render($data); $expected = [ 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'], @@ -513,7 +513,7 @@ public function testRenderEmptyOption() { ]; $this->assertTags($result, $expected); - $data['selected'] = false; + $data['val'] = false; $result = $select->render($data); $this->assertTags($result, $expected); }