Skip to content

Commit

Permalink
Improve templateVars support for radios and add tests.
Browse files Browse the repository at this point in the history
Allow the top-level templatevars to be used for the wrapper, while each
input can use the complex form to set its own templateVars. Individual
radio buttons will also inherit template vars from the top level key.
  • Loading branch information
markstory committed Jun 30, 2015
1 parent 2cedf1a commit bcc2d93
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/View/Widget/RadioWidget.php
Expand Up @@ -151,24 +151,27 @@ protected function _renderInput($val, $text, $data, $context)
$escape = $data['escape'];
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 (!isset($radio['templateVars'])) {
$radio['templateVars'] = [];
}
if (!empty($data['templateVars'])) {
$radio['templateVars'] = array_merge($data['templateVars'], $radio['templateVars']);
}

if (empty($radio['id'])) {
$radio['id'] = $this->_id($radio['name'], $radio['value']);
}

if (isset($data['val']) && is_bool($data['val'])) {
$data['val'] = $data['val'] ? 1 : 0;
}

if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) {
$radio['checked'] = true;
}

if ($this->_isDisabled($radio, $data['disabled'])) {
$radio['disabled'] = true;
}
Expand All @@ -182,7 +185,7 @@ protected function _renderInput($val, $text, $data, $context)
$input = $this->_templates->format('radio', [
'name' => $radio['name'],
'value' => $escape ? h($radio['value']) : $radio['value'],
'templateVars' => $data['templateVars'],
'templateVars' => $radio['templateVars'],
'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']),
]);

Expand Down Expand Up @@ -230,6 +233,7 @@ protected function _renderLabel($radio, $label, $input, $context, $escape)
'for' => $radio['id'],
'escape' => $escape,
'text' => $radio['text'],
'templateVars' => $radio['templateVars'],
'input' => $input,
];
return $this->_label->render($labelAttrs, $context);
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/View/Widget/RadioWidgetTest.php
Expand Up @@ -657,4 +657,35 @@ public function testRenderContainerTemplate()
$result
);
}

/**
* Ensure that template vars work.
*
* @return void
*/
public function testRenderTemplateVars()
{
$this->templates->add([
'radioWrapper' => '<div class="radio" data-var="{{wrapperVar}}">{{label}}</div>',
'radio' => '<input type="radio" data-i="{{inputVar}}" name="{{name}}" value="{{value}}"{{attrs}}>',
'nestingLabel' => '<label{{attrs}}>{{input}}{{text}} {{labelVar}} {{wrapperVar}}</label>',
]);
$label = new NestingLabelWidget($this->templates);
$radio = new RadioWidget($this->templates, $label);
$data = [
'name' => 'Versions[ver]',
'options' => [
['value' => '1x', 'text' => 'one x', 'templateVars' => ['labelVar' => 'l-var', 'inputVar' => 'i-var']],
'2' => 'two',
],
'templateVars' => [
'wrapperVar' => 'wrap-var',
]
];
$result = $radio->render($data, $this->context);
$this->assertContains('data-var="wrap-var"><label', $result);
$this->assertContains('type="radio" data-i="i-var"', $result);
$this->assertContains('one x l-var wrap-var</label>', $result);
$this->assertContains('two wrap-var</label>', $result);
}
}

0 comments on commit bcc2d93

Please sign in to comment.