diff --git a/Cake/View/Input/Radio.php b/Cake/View/Input/Radio.php index 2476cba12d5..05fd07cd99c 100644 --- a/Cake/View/Input/Radio.php +++ b/Cake/View/Input/Radio.php @@ -71,7 +71,12 @@ public function render($data) { $options = (array)$data['options']; $escape = $data['escape']; foreach ($options as $val => $text) { - $radio = ['value' => $val]; + if (is_int($val) && isset($text['text'], $text['value'])) { + $radio = $text; + $text = $radio['text']; + } else { + $radio = ['value' => $val, 'text' => $text]; + } $radio['name'] = $data['name']; if (empty($radio['id'])) { @@ -80,14 +85,14 @@ public function render($data) { $labelAttrs = ['for' => $radio['id'], 'escape' => $escape]; $label = $this->_templates->format('label', [ - 'text' => $escape ? h($text) : $text, + 'text' => $escape ? h($radio['text']) : $radio['text'], 'attrs' => $this->_templates->formatAttributes($labelAttrs), ]); $input = $this->_templates->format('radio', [ 'name' => $radio['name'], - 'value' => $escape ? h($val) : $val, - 'attrs' => $this->_templates->formatAttributes($radio, ['value', 'name']), + 'value' => $escape ? h($radio['value']) : $radio['value'], + 'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']), ]); $opts[] = $this->_templates->format('radioContainer', [ diff --git a/Test/TestCase/View/Input/RadioTest.php b/Test/TestCase/View/Input/RadioTest.php index b41b1e1b942..8dae585f22f 100644 --- a/Test/TestCase/View/Input/RadioTest.php +++ b/Test/TestCase/View/Input/RadioTest.php @@ -69,8 +69,43 @@ public function testRenderSimple() { $this->assertTags($result, $expected); } +/** + * Test rendering inputs with the complex option form. + * + * @return void + */ public function testRenderComplex() { - $this->markTestIncomplete(); + $radio = new Radio($this->templates); + $data = [ + 'name' => 'Crayons[color]', + 'options' => [ + ['value' => 'r', 'text' => 'Red', 'id' => 'my_id'], + ['value' => 'b', 'text' => 'Black', 'id' => 'my_id_2', 'data-test' => 'test'], + ] + ]; + $result = $radio->render($data); + $expected = [ + ['input' => [ + 'type' => 'radio', + 'name' => 'Crayons[color]', + 'value' => 'r', + 'id' => 'my_id' + ]], + ['label' => ['for' => 'my_id']], + 'Red', + '/label', + ['input' => [ + 'type' => 'radio', + 'name' => 'Crayons[color]', + 'value' => 'b', + 'id' => 'my_id_2', + 'data-test' => 'test' + ]], + ['label' => ['for' => 'my_id_2']], + 'Black', + '/label', + ]; + $this->assertTags($result, $expected); } public function testRenderSelected() {