Browse Source

Improve and add tests for MultiCheckbox templateVars.

Support inheriting template vars like select and radio widgets do.
Mark Story 10 years ago
parent
commit
0ec2ca9bea

+ 9 - 1
src/View/Widget/MultiCheckboxWidget.php

@@ -121,11 +121,16 @@ class MultiCheckboxWidget implements WidgetInterface
             $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'];
 
@@ -156,6 +161,7 @@ class MultiCheckboxWidget implements WidgetInterface
         $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']
@@ -166,6 +172,7 @@ class MultiCheckboxWidget implements WidgetInterface
             'for' => $checkbox['id'],
             'escape' => $checkbox['escape'],
             'text' => $checkbox['text'],
+            'templateVars' => $checkbox['templateVars'],
             'input' => $input,
         ];
         if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) {
@@ -174,6 +181,7 @@ class MultiCheckboxWidget implements WidgetInterface
         $label = $this->_label->render($labelAttrs, $context);
 
         return $this->_templates->format('checkboxWrapper', [
+            'templateVars' => $checkbox['templateVars'],
             'label' => $label,
             'input' => $input
         ]);

+ 54 - 0
tests/TestCase/View/Widget/MultiCheckboxWidgetTest.php

@@ -312,4 +312,58 @@ class MultiCheckboxWidgetTest extends TestCase
         ];
         $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);
+    }
 }