Skip to content

Commit

Permalink
Add checked + disabled support to radio buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 11, 2014
1 parent ee98664 commit 8b53be5
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 2 deletions.
26 changes: 26 additions & 0 deletions Cake/View/Input/Radio.php
Expand Up @@ -83,6 +83,14 @@ public function render($data) {
$radio['id'] = Inflector::slug($radio['name'] . '_' . $radio['value']);
}

if (isset($data['value']) && strval($data['value']) === strval($radio['value'])) {
$radio['checked'] = true;
}

if ($this->_isDisabled($radio, $data['disabled'])) {
$radio['disabled'] = true;
}

$labelAttrs = ['for' => $radio['id'], 'escape' => $escape];
$label = $this->_templates->format('label', [
'text' => $escape ? h($radio['text']) : $radio['text'],
Expand All @@ -103,4 +111,22 @@ public function render($data) {
return implode('', $opts);
}

/**
* Disabled attribute detection.
*
* @param array $radio
* @param array|null|true $disabled
* @return boolean
*/
protected function _isDisabled($radio, $disabled) {
if (!$disabled) {
return false;
}
if ($disabled === true) {
return true;
}
$isNumeric = is_numeric($radio['value']);
return (!is_array($disabled) || in_array((string)$radio['value'], $disabled, !$isNumeric));
}

}
124 changes: 122 additions & 2 deletions Test/TestCase/View/Input/RadioTest.php
Expand Up @@ -108,18 +108,138 @@ public function testRenderComplex() {
$this->assertTags($result, $expected);
}

/**
* test render() and selected inputs.
*
* @return void
*/
public function testRenderSelected() {
$this->markTestIncomplete();
$radio = new Radio($this->templates);
$data = [
'name' => 'Versions[ver]',
'value' => '1',
'options' => [
1 => 'one',
'1x' => 'one x',
'2' => 'two',
]
];
$result = $radio->render($data);
$expected = [
['input' => [
'id' => 'Versions_ver_1',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1',
'checked' => 'checked'
]],
['label' => ['for' => 'Versions_ver_1']],
'one',
'/label',
['input' => [
'id' => 'Versions_ver_1x',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1x'
]],
['label' => ['for' => 'Versions_ver_1x']],
'one x',
'/label',
['input' => [
'id' => 'Versions_ver_2',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '2'
]],
['label' => ['for' => 'Versions_ver_2']],
'two',
'/label',
];
$this->assertTags($result, $expected);
}

/**
* Test rendering with disable inputs
*
* @return void
*/
public function testRenderDisabled() {
$this->markTestIncomplete();
$radio = new Radio($this->templates);
$data = [
'name' => 'Versions[ver]',
'options' => [
1 => 'one',
'1x' => 'one x',
'2' => 'two',
],
'disabled' => true,
];
$result = $radio->render($data);
$expected = [
['input' => [
'id' => 'Versions_ver_1',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1',
'disabled' => 'disabled'
]],
['label' => ['for' => 'Versions_ver_1']],
'one',
'/label',
['input' => [
'id' => 'Versions_ver_1x',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1x',
'disabled' => 'disabled'
]],
['label' => ['for' => 'Versions_ver_1x']],
'one x',
'/label',
];
$this->assertTags($result, $expected);

$data['disabled'] = ['1'];
$result = $radio->render($data);
$expected = [
['input' => [
'id' => 'Versions_ver_1',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1',
'disabled' => 'disabled'
]],
['label' => ['for' => 'Versions_ver_1']],
'one',
'/label',
['input' => [
'id' => 'Versions_ver_1x',
'name' => 'Versions[ver]',
'type' => 'radio',
'value' => '1x',
]],
['label' => ['for' => 'Versions_ver_1x']],
'one x',
'/label',
];
$this->assertTags($result, $expected);
}

/**
* Test rendering with label options.
*
* @return void
*/
public function testRenderLabelOptions() {
$this->markTestIncomplete();
}

/**
* Ensure that the input + label are composed with
* a template.
*
* @return void
*/
public function testRenderContainerTemplate() {
$this->markTestIncomplete();
}
Expand Down

0 comments on commit 8b53be5

Please sign in to comment.