Skip to content
Permalink
Browse files

Fix merging of label options in MultiCheckboxWidget.

This allows generating checkboxes without inputs nested inside labels.
  • Loading branch information...
ADmad committed Oct 5, 2018
1 parent 95222a0 commit f2cdd62b278d6443521d90d9f0ddccfc11356b01
Showing with 40 additions and 5 deletions.
  1. +2 −5 src/View/Widget/MultiCheckboxWidget.php
  2. +38 −0 tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php
@@ -194,18 +194,15 @@ protected function _renderInput($checkbox, $context)
if ($checkbox['label'] === false && strpos($this->_templates->get('checkboxWrapper'), '{{input}}') === false) {
$label = $input;
} else {
$labelAttrs = [
$labelAttrs = is_array($checkbox['label']) ? $checkbox['label'] : [];
$labelAttrs += [
'for' => $checkbox['id'],
'escape' => $checkbox['escape'],
'text' => $checkbox['text'],
'templateVars' => $checkbox['templateVars'],
'input' => $input
];
if (is_array($checkbox['label'])) {
$labelAttrs += $checkbox['label'];
}
if ($checkbox['checked']) {
$labelAttrs = $this->_templates->addClass($labelAttrs, 'selected');
}
@@ -18,6 +18,7 @@
use Cake\View\StringTemplate;
use Cake\View\Widget\LabelWidget;
use Cake\View\Widget\MultiCheckboxWidget;
use Cake\View\Widget\NestingLabelWidget;
/**
* MultiCheckbox test case.
@@ -36,6 +37,7 @@ public function setUp()
$templates = [
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'label' => '<label{{attrs}}>{{text}}</label>',
'nestingLabel' => '<label{{attrs}}>{{input}}{{text}}</label>',
'checkboxWrapper' => '<div class="checkbox">{{input}}{{label}}</div>',
'multicheckboxWrapper' => '<fieldset{{attrs}}>{{content}}</fieldset>',
'multicheckboxTitle' => '<legend>{{text}}</legend>',
@@ -440,6 +442,42 @@ public function testNoLabelWithCheckboxWrapperOption()
$this->assertHtml($expected, $result);
}
/**
* Test rendering without input nesting inspite of using NestingLabelWidget
*
* @return void
*/
public function testRenderNestingLabelWidgetWithoutInputNesting()
{
$label = new NestingLabelWidget($this->templates);
$input = new MultiCheckboxWidget($this->templates, $label);
$data = [
'name' => 'tags',
'label' => [
'input' => false,
],
'options' => [
1 => 'CakePHP',
],
];
$result = $input->render($data, $this->context);
$expected = [
['div' => ['class' => 'checkbox']],
['input' => [
'type' => 'checkbox',
'name' => 'tags[]',
'value' => 1,
'id' => 'tags-1',
]],
['label' => ['for' => 'tags-1']],
'CakePHP',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
}
/**
* Test render with groupings.
*

0 comments on commit f2cdd62

Please sign in to comment.
You can’t perform that action at this time.