Skip to content

Commit

Permalink
Use 'val' instead of 'selected'.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Jan 13, 2014
1 parent a2a4c8c commit 88a3cca
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/View/Input/Checkbox.php
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/View/Input/Radio.php
Expand Up @@ -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.
*
Expand All @@ -73,7 +73,7 @@ public function render($data) {
'name' => '',
'options' => [],
'disabled' => null,
'selected' => null,
'val' => null,
'escape' => true,
'label' => true,
'empty' => false,
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/View/Input/SelectBox.php
Expand Up @@ -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.
Expand Down Expand Up @@ -121,15 +121,15 @@ public function render($data) {
'escape' => true,
'options' => [],
'disabled' => null,
'selected' => null,
'val' => null,
];

if (empty($data['name'])) {
throw new \RuntimeException('Cannot make inputs with empty name attributes.');
}
$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']);
}
Expand Down Expand Up @@ -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'];
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/View/Input/CheckboxTest.php
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Input/RadioTest.php
Expand Up @@ -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',
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -141,7 +141,7 @@ public function testRenderSelected() {
$data = [
'id' => 'BirdName',
'name' => 'Birds[name]',
'selected' => '1',
'val' => '1',
'options' => [
1 => 'one',
'1x' => 'one x',
Expand All @@ -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'],
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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 = [
Expand All @@ -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',
Expand Down Expand Up @@ -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'],
Expand All @@ -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);
}
Expand Down

0 comments on commit 88a3cca

Please sign in to comment.