Skip to content

Commit

Permalink
Fix merging of label options in MultiCheckboxWidget.
Browse files Browse the repository at this point in the history
This allows generating checkboxes without inputs nested inside labels.
  • Loading branch information
ADmad committed Oct 5, 2018
1 parent 95222a0 commit f2cdd62
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/View/Widget/MultiCheckboxWidget.php
Expand Up @@ -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');
}
Expand Down
38 changes: 38 additions & 0 deletions tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php
Expand Up @@ -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.
Expand All @@ -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>',
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit f2cdd62

Please sign in to comment.