Skip to content

Commit

Permalink
Improve and add tests for MultiCheckbox templateVars.
Browse files Browse the repository at this point in the history
Support inheriting template vars like select and radio widgets do.
  • Loading branch information
markstory committed Jun 30, 2015
1 parent f7d6dfc commit 0ec2ca9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/View/Widget/MultiCheckboxWidget.php
Expand Up @@ -121,11 +121,16 @@ public function render(array $data, ContextInterface $context)
$checkbox = [
'value' => $key,
'text' => $val,
'templateVars' => $data['templateVars']
];
if (is_array($val) && isset($val['text'], $val['value'])) {
$checkbox = $val;
}
if (!isset($checkbox['templateVars'])) {
$checkbox['templateVars'] = $data['templateVars'];
}
if (!empty($data['templateVars'])) {
$checkbox['templateVars'] = array_merge($data['templateVars'], $checkbox['templateVars']);
}
$checkbox['name'] = $data['name'];
$checkbox['escape'] = $data['escape'];

Expand Down Expand Up @@ -156,6 +161,7 @@ protected function _renderInput($checkbox, $context)
$input = $this->_templates->format('checkbox', [
'name' => $checkbox['name'] . '[]',
'value' => $checkbox['escape'] ? h($checkbox['value']) : $checkbox['value'],
'templateVars' => $checkbox['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$checkbox,
['name', 'value', 'text']
Expand All @@ -166,6 +172,7 @@ protected function _renderInput($checkbox, $context)
'for' => $checkbox['id'],
'escape' => $checkbox['escape'],
'text' => $checkbox['text'],
'templateVars' => $checkbox['templateVars'],
'input' => $input,
];
if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) {
Expand All @@ -174,6 +181,7 @@ protected function _renderInput($checkbox, $context)
$label = $this->_label->render($labelAttrs, $context);

return $this->_templates->format('checkboxWrapper', [
'templateVars' => $checkbox['templateVars'],
'label' => $label,
'input' => $input
]);
Expand Down
54 changes: 54 additions & 0 deletions tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php
Expand Up @@ -312,4 +312,58 @@ public function testRenderDisabled()
];
$this->assertHtml($expected, $result);
}

/**
* Test render templateVars
*
* @return void
*/
public function testRenderTemplateVars()
{
$templates = [
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}" data-var="{{inputVar}}" {{attrs}}>',
'label' => '<label{{attrs}}>{{text}} {{inputVar}}</label>',
'checkboxWrapper' => '<div class="checkbox" data-wrap="{{wrapVar}}">{{input}}{{label}}</div>',
];
$this->templates->add($templates);

$label = new LabelWidget($this->templates);
$input = new MultiCheckboxWidget($this->templates, $label);
$data = [
'name' => 'Tags[id]',
'options' => [
['value' => '1', 'text' => 'CakePHP', 'templateVars' => ['inputVar' => 'i-var']],
'1x' => 'Development',
],
'templateVars' => ['inputVar' => 'default', 'wrapVar' => 'val'],
];
$result = $input->render($data, $this->context);
$expected = [
['div' => ['class' => 'checkbox', 'data-wrap' => 'val']],
['input' => [
'type' => 'checkbox',
'name' => 'Tags[id][]',
'value' => 1,
'id' => 'tags-id-1',
'data-var' => 'i-var',
]],
['label' => ['for' => 'tags-id-1']],
'CakePHP i-var',
'/label',
'/div',
['div' => ['class' => 'checkbox', 'data-wrap' => 'val']],
['input' => [
'type' => 'checkbox',
'name' => 'Tags[id][]',
'value' => '1x',
'id' => 'tags-id-1x',
'data-var' => 'default'
]],
['label' => ['for' => 'tags-id-1x']],
'Development default',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
}
}

0 comments on commit 0ec2ca9

Please sign in to comment.