From 116f799a4efcc3ad12f87c876f9b9c029e6f1a29 Mon Sep 17 00:00:00 2001 From: lilHermit Date: Wed, 8 Feb 2017 16:19:47 +0000 Subject: [PATCH] Allow attributes to be passed to RadioWidget & MultiCheckboxWidget This currently already works with CheckboxWidget --- src/View/Widget/MultiCheckboxWidget.php | 4 +- src/View/Widget/RadioWidget.php | 2 +- tests/TestCase/View/Helper/FormHelperTest.php | 82 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/View/Widget/MultiCheckboxWidget.php b/src/View/Widget/MultiCheckboxWidget.php index f417522a404..b242028b853 100644 --- a/src/View/Widget/MultiCheckboxWidget.php +++ b/src/View/Widget/MultiCheckboxWidget.php @@ -167,7 +167,7 @@ protected function _renderInputs($data, $context) if (empty($checkbox['id'])) { $checkbox['id'] = $this->_id($checkbox['name'], $checkbox['value']); } - $out[] = $this->_renderInput($checkbox, $context); + $out[] = $this->_renderInput($checkbox + $data, $context); } return $out; @@ -188,7 +188,7 @@ protected function _renderInput($checkbox, $context) 'templateVars' => $checkbox['templateVars'], 'attrs' => $this->_templates->formatAttributes( $checkbox, - ['name', 'value', 'text'] + ['name', 'value', 'text', 'options', 'label', 'val', 'type', 'hiddenField'] ) ]); diff --git a/src/View/Widget/RadioWidget.php b/src/View/Widget/RadioWidget.php index e91cf570b84..90345b74014 100644 --- a/src/View/Widget/RadioWidget.php +++ b/src/View/Widget/RadioWidget.php @@ -187,7 +187,7 @@ protected function _renderInput($val, $text, $data, $context) 'name' => $radio['name'], 'value' => $escape ? h($radio['value']) : $radio['value'], 'templateVars' => $radio['templateVars'], - 'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text']), + 'attrs' => $this->_templates->formatAttributes($radio + $data, ['name', 'value', 'text', 'options', 'label', 'val', 'type']), ]); $label = $this->_renderLabel( diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 3f9f49b0098..6b18ddb1c03 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -8496,4 +8496,86 @@ public function testNestedLabelInput() ]; $this->assertHtml($expected, $result); } + + /** + * testRadioAttributes method + * + * Test generation of radio widget with additional attributes. + * + * @return void + */ + public function testRadioAttributes() + { + $result = $this->Form->radio('Model.field', ['option A'], ['class' => 'my-class', 'data-ref' => 'custom-attr']); + $expected = [ + 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''], + 'label' => ['for' => 'model-field-0'], + ['input' => [ + 'type' => 'radio', 'name' => 'Model[field]', + 'value' => '0', 'id' => 'model-field-0', + 'class' => 'my-class', 'data-ref' => 'custom-attr' + ]], + 'option A', + '/label' + ]; + $this->assertHtml($expected, $result); + } + + /** + * testCheckboxAttributes method + * + * Test generation of checkbox widget with additional attributes. + * + * @return void + */ + public function testCheckboxAttributes() + { + $result = $this->Form->checkbox('Model.field', ['class' => 'my-class', 'data-ref' => 'custom-attr']); + $expected = [ + 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'], + ['input' => [ + 'type' => 'checkbox', 'name' => 'Model[field]', + 'value' => '1', 'class' => 'my-class', + 'data-ref' => 'custom-attr' + ]] + ]; + $this->assertHtml($expected, $result); + } + + /** + * testMultiCheckboxAttributes method + * + * Test generation of multiple checkboxes with additional attributes + * + * @return void + */ + public function testMultiCheckboxAttributes() + { + $result = $this->Form->multiCheckbox('category', ['1', '2'], ['class' => 'my-class', 'data-ref' => 'custom-attr']); + + $expected = [ + 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''], + ['div' => ['class' => 'checkbox']], + ['label' => ['for' => 'category-0']], + ['input' => [ + 'type' => 'checkbox', 'name' => 'category[]', + 'value' => '0', 'id' => 'category-0', + 'class' => 'my-class', 'data-ref' => 'custom-attr' + ]], + '1', + '/label', + '/div', + ['div' => ['class' => 'checkbox']], + ['label' => ['for' => 'category-1']], + ['input' => [ + 'type' => 'checkbox', 'name' => 'category[]', + 'value' => '1', 'id' => 'category-1', + 'class' => 'my-class', 'data-ref' => 'custom-attr' + ]], + '2', + '/label', + '/div' + ]; + $this->assertHtml($expected, $result); + } }