Skip to content
Permalink
Browse files

Add checked + disabled support to radio buttons.

  • Loading branch information...
markstory committed Jan 9, 2014
1 parent ee98664 commit 8b53be5e4d5fdfa42722a57766b5fb45c8238cc5
Showing with 148 additions and 2 deletions.
  1. +26 −0 Cake/View/Input/Radio.php
  2. +122 −2 Test/TestCase/View/Input/RadioTest.php
@@ -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'],
@@ -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));
}
}
@@ -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();
}

0 comments on commit 8b53be5

Please sign in to comment.
You can’t perform that action at this time.