Skip to content

Commit

Permalink
Add empty option support.
Browse files Browse the repository at this point in the history
Add the empty option that FormHelper currently supports.
  • Loading branch information
markstory committed Jan 11, 2014
1 parent 52f16a9 commit c790424
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cake/View/Input/Radio.php
Expand Up @@ -66,10 +66,17 @@ public function render($data) {
'value' => null,
'escape' => true,
'label' => true,
'empty' => false,
];
$opts = [];
$options = (array)$data['options'];
$escape = $data['escape'];
if (!empty($data['empty'])) {
$empty = $data['empty'] === true ? 'empty' : $data['empty'];
$options = ['' => $empty] + $options;
}
unset($data['empty']);

foreach ($options as $val => $text) {
if (is_int($val) && isset($text['text'], $text['value'])) {
$radio = $text;
Expand Down
60 changes: 60 additions & 0 deletions Test/TestCase/View/Input/RadioTest.php
Expand Up @@ -108,6 +108,66 @@ public function testRenderComplex() {
$this->assertTags($result, $expected);
}

/**
* Test rendering the empty option.
*
* @return void
*/
public function testRenderEmptyOption() {
$radio = new Radio($this->templates);
$data = [
'name' => 'Crayons[color]',
'options' => ['r' => 'Red'],
'empty' => true,
];
$result = $radio->render($data);
$expected = [
['input' => [
'type' => 'radio',
'name' => 'Crayons[color]',
'value' => '',
'id' => 'Crayons_color'
]],
['label' => ['for' => 'Crayons_color']],
'empty',
'/label',
['input' => [
'type' => 'radio',
'name' => 'Crayons[color]',
'value' => 'r',
'id' => 'Crayons_color_r'
]],
['label' => ['for' => 'Crayons_color_r']],
'Red',
'/label',
];
$this->assertTags($result, $expected);

$data['empty'] = 'Choose one';
$result = $radio->render($data);
$expected = [
['input' => [
'type' => 'radio',
'name' => 'Crayons[color]',
'value' => '',
'id' => 'Crayons_color'
]],
['label' => ['for' => 'Crayons_color']],
'Choose one',
'/label',
['input' => [
'type' => 'radio',
'name' => 'Crayons[color]',
'value' => 'r',
'id' => 'Crayons_color_r'
]],
['label' => ['for' => 'Crayons_color_r']],
'Red',
'/label',
];
$this->assertTags($result, $expected);
}

/**
* test render() and selected inputs.
*
Expand Down

0 comments on commit c790424

Please sign in to comment.