Skip to content

Commit

Permalink
Add support for complex input type format.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 11, 2014
1 parent 293039c commit ee98664
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
13 changes: 9 additions & 4 deletions Cake/View/Input/Radio.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public function render($data) {
$options = (array)$data['options']; $options = (array)$data['options'];
$escape = $data['escape']; $escape = $data['escape'];
foreach ($options as $val => $text) { 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']; $radio['name'] = $data['name'];


if (empty($radio['id'])) { if (empty($radio['id'])) {
Expand All @@ -80,14 +85,14 @@ public function render($data) {


$labelAttrs = ['for' => $radio['id'], 'escape' => $escape]; $labelAttrs = ['for' => $radio['id'], 'escape' => $escape];
$label = $this->_templates->format('label', [ $label = $this->_templates->format('label', [
'text' => $escape ? h($text) : $text, 'text' => $escape ? h($radio['text']) : $radio['text'],
'attrs' => $this->_templates->formatAttributes($labelAttrs), 'attrs' => $this->_templates->formatAttributes($labelAttrs),
]); ]);


$input = $this->_templates->format('radio', [ $input = $this->_templates->format('radio', [
'name' => $radio['name'], 'name' => $radio['name'],
'value' => $escape ? h($val) : $val, 'value' => $escape ? h($radio['value']) : $radio['value'],
'attrs' => $this->_templates->formatAttributes($radio, ['value', 'name']), 'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']),
]); ]);


$opts[] = $this->_templates->format('radioContainer', [ $opts[] = $this->_templates->format('radioContainer', [
Expand Down
37 changes: 36 additions & 1 deletion Test/TestCase/View/Input/RadioTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,8 +69,43 @@ public function testRenderSimple() {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
} }


/**
* Test rendering inputs with the complex option form.
*
* @return void
*/
public function testRenderComplex() { 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() { public function testRenderSelected() {
Expand Down

0 comments on commit ee98664

Please sign in to comment.