Skip to content

Commit 88a3cca

Browse files
committed
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.
1 parent a2a4c8c commit 88a3cca

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

src/View/Input/Checkbox.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function __construct($templates) {
4242
*
4343
* - `name` - The name of the input.
4444
* - `value` - The value attribute. Defaults to '1'.
45-
* - `checked` - Whether or not the checkbox should be checked.
45+
* - `val` - The current value. If it matches `value` the checkbox will be checked.
46+
* You can also use the 'checked' attribute to make the checkbox checked.
4647
* - `disabled` - Whether or not the checkbox should be disabled.
4748
*
4849
* Any other attributes passed in will be treated as HTML attributes.
@@ -54,14 +55,14 @@ public function render($data) {
5455
$data += [
5556
'name' => '',
5657
'value' => 1,
57-
'selected' => null,
58+
'val' => null,
5859
'checked' => false,
5960
'disabled' => false,
6061
];
6162
if ($this->_isChecked($data)) {
6263
$data['checked'] = true;
6364
}
64-
unset($data['selected']);
65+
unset($data['val']);
6566

6667
$attrs = $this->_templates->formatAttributes(
6768
$data,
@@ -85,7 +86,7 @@ protected function _isChecked($data) {
8586
if (!empty($data['checked'])) {
8687
return true;
8788
}
88-
if ((string)$data['selected'] === (string)$data['value']) {
89+
if ((string)$data['val'] === (string)$data['value']) {
8990
return true;
9091
}
9192
return false;

src/View/Input/Radio.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct($templates) {
6161
* - `options` - An array of options. See below for more information.
6262
* - `disabled` - Either true or an array of inputs to disable.
6363
* When true, the select element will be disabled.
64-
* - `selected` - A string of the option to mark as selected.
64+
* - `val` - A string of the option to mark as selected.
6565
* - `label` - Either false to disable label generation, or
6666
* an array of attributes for all labels.
6767
*
@@ -73,7 +73,7 @@ public function render($data) {
7373
'name' => '',
7474
'options' => [],
7575
'disabled' => null,
76-
'selected' => null,
76+
'val' => null,
7777
'escape' => true,
7878
'label' => true,
7979
'empty' => false,
@@ -137,7 +137,7 @@ protected function _renderInput($val, $text, $data) {
137137
$radio['id'] = Inflector::slug($radio['name'] . '_' . $radio['value']);
138138
}
139139

140-
if (isset($data['selected']) && strval($data['selected']) === strval($radio['value'])) {
140+
if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) {
141141
$radio['checked'] = true;
142142
}
143143

src/View/Input/SelectBox.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct($templates) {
5151
* - `options` - An array of options.
5252
* - `disabled` - Either true or an array of options to disable.
5353
* When true, the select element will be disabled.
54-
* - `selected` - Either a string or an array of options to mark as selected.
54+
* - `val` - Either a string or an array of options to mark as selected.
5555
* - `empty` - Set to true to add an empty option at the top of the
5656
* option elements. Set to a string to define the display value of the
5757
* empty option.
@@ -121,15 +121,15 @@ public function render($data) {
121121
'escape' => true,
122122
'options' => [],
123123
'disabled' => null,
124-
'selected' => null,
124+
'val' => null,
125125
];
126126

127127
if (empty($data['name'])) {
128128
throw new \RuntimeException('Cannot make inputs with empty name attributes.');
129129
}
130130
$options = $this->_renderContent($data);
131131
$name = $data['name'];
132-
unset($data['name'], $data['options'], $data['empty'], $data['selected'], $data['escape']);
132+
unset($data['name'], $data['options'], $data['empty'], $data['val'], $data['escape']);
133133
if (isset($data['disabled']) && is_array($data['disabled'])) {
134134
unset($data['disabled']);
135135
}
@@ -168,7 +168,7 @@ protected function _renderContent($data) {
168168
return [];
169169
}
170170

171-
$selected = isset($data['selected']) ? $data['selected'] : null;
171+
$selected = isset($data['val']) ? $data['val'] : null;
172172
$disabled = null;
173173
if (isset($data['disabled']) && is_array($data['disabled'])) {
174174
$disabled = $data['disabled'];

tests/TestCase/View/Input/CheckboxTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ public function testRenderChecked() {
116116
$data = [
117117
'name' => 'Comment[spam]',
118118
'value' => 1,
119-
'selected' => 1,
119+
'val' => 1,
120120
];
121121
$result = $checkbox->render($data);
122122
$this->assertTags($result, $expected);
123123

124-
$data['selected'] = '1';
124+
$data['val'] = '1';
125125
$result = $checkbox->render($data);
126126
$this->assertTags($result, $expected);
127127

128128
$data = [
129129
'name' => 'Comment[spam]',
130130
'value' => 1,
131-
'selected' => '1x',
131+
'val' => '1x',
132132
];
133133
$result = $checkbox->render($data);
134134
$expected = [

tests/TestCase/View/Input/RadioTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function testRenderSelected() {
215215
$radio = new Radio($this->templates);
216216
$data = [
217217
'name' => 'Versions[ver]',
218-
'selected' => '1',
218+
'val' => '1',
219219
'options' => [
220220
1 => 'one',
221221
'1x' => 'one x',

tests/TestCase/View/Input/SelectBoxTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function testRenderSelected() {
141141
$data = [
142142
'id' => 'BirdName',
143143
'name' => 'Birds[name]',
144-
'selected' => '1',
144+
'val' => '1',
145145
'options' => [
146146
1 => 'one',
147147
'1x' => 'one x',
@@ -160,7 +160,7 @@ public function testRenderSelected() {
160160
];
161161
$this->assertTags($result, $expected);
162162

163-
$data['selected'] = 2;
163+
$data['val'] = 2;
164164
$result = $select->render($data);
165165
$expected = [
166166
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
@@ -211,7 +211,7 @@ public function testRenderMultipleSelected() {
211211
'multiple' => true,
212212
'id' => 'BirdName',
213213
'name' => 'Birds[name]',
214-
'selected' => ['1', '2', 'burp'],
214+
'val' => ['1', '2', 'burp'],
215215
'options' => [
216216
1 => 'one',
217217
'1x' => 'one x',
@@ -373,7 +373,7 @@ public function testRenderOptionGroupsSelectedAndDisabled() {
373373
$select = new SelectBox($this->templates);
374374
$data = [
375375
'name' => 'Birds[name]',
376-
'selected' => ['1', '2', 'burp'],
376+
'val' => ['1', '2', 'burp'],
377377
'disabled' => ['1x', '2x', 'nope'],
378378
'options' => [
379379
'ones' => [
@@ -416,7 +416,7 @@ public function testRenderDisabled() {
416416
'disabled' => true,
417417
'name' => 'Birds[name]',
418418
'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
419-
'selected' => 'a',
419+
'val' => 'a',
420420
];
421421
$result = $select->render($data);
422422
$expected = [
@@ -440,7 +440,7 @@ public function testRenderDisabledMultiple() {
440440
$select = new SelectBox($this->templates);
441441
$data = [
442442
'disabled' => ['a', 'c'],
443-
'selected' => 'a',
443+
'val' => 'a',
444444
'name' => 'Birds[name]',
445445
'options' => [
446446
'a' => 'Albatross',
@@ -502,7 +502,7 @@ public function testRenderEmptyOption() {
502502
$this->assertTags($result, $expected);
503503

504504
$data['empty'] = 'empty';
505-
$data['selected'] = '';
505+
$data['val'] = '';
506506
$result = $select->render($data);
507507
$expected = [
508508
'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
@@ -513,7 +513,7 @@ public function testRenderEmptyOption() {
513513
];
514514
$this->assertTags($result, $expected);
515515

516-
$data['selected'] = false;
516+
$data['val'] = false;
517517
$result = $select->render($data);
518518
$this->assertTags($result, $expected);
519519
}

0 commit comments

Comments
 (0)