From c790424670a2ebc72c38ae6eb961f3b9afacb17f Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 9 Jan 2014 22:02:02 -0500 Subject: [PATCH] Add empty option support. Add the empty option that FormHelper currently supports. --- Cake/View/Input/Radio.php | 7 +++ Test/TestCase/View/Input/RadioTest.php | 60 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/Cake/View/Input/Radio.php b/Cake/View/Input/Radio.php index 2a9e4a2ee1f..1495251fd8f 100644 --- a/Cake/View/Input/Radio.php +++ b/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; diff --git a/Test/TestCase/View/Input/RadioTest.php b/Test/TestCase/View/Input/RadioTest.php index b28d36c43a1..094871a00ff 100644 --- a/Test/TestCase/View/Input/RadioTest.php +++ b/Test/TestCase/View/Input/RadioTest.php @@ -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. *