Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add empty option support.

Add the empty option that FormHelper currently supports.
  • Loading branch information...
commit c790424670a2ebc72c38ae6eb961f3b9afacb17f 1 parent 52f16a9
@markstory markstory authored
View
7 Cake/View/Input/Radio.php
@@ -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;
View
60 Test/TestCase/View/Input/RadioTest.php
@@ -109,6 +109,66 @@ public function testRenderComplex() {
}
/**
+ * 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.
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.