From eeefa03546260a7f60faf343efd9ea9bb00036af Mon Sep 17 00:00:00 2001 From: xhs345 Date: Thu, 19 May 2016 17:28:47 -0700 Subject: [PATCH] Updated Radio and Inputs form helper Also added UnitTest for radio fieldset class-name --- .../Test/Case/View/Helper/FormHelperTest.php | 19 +++++++++++++ lib/Cake/View/Helper/FormHelper.php | 27 ++++++++++--------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index c2627b8d3..7db56739a 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -3914,6 +3914,25 @@ class FormHelperTest extends CakeTestCase { ); $this->assertTags($result, $expected); + $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('fieldset' => 'classy-stuff')); + $expected = array( + 'fieldset' => array('class' => 'classy-stuff'), + 'legend' => array(), + 'Field', + '/legend', + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'), + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')), + array('label' => array('for' => 'ModelField0')), + 'option A', + '/label', + array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')), + array('label' => array('for' => 'ModelField1')), + 'option B', + '/label', + '/fieldset' + ); + $this->assertTags($result, $expected); + $result = $this->Form->radio( 'Employee.gender', array('male' => 'Male', 'female' => 'Female'), diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 974c54c65..4710b8977 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -932,11 +932,15 @@ class FormHelper extends AppHelper { if (isset($options['legend'])) { $legend = $options['legend']; + unset($options['legend']); } + if (isset($options['fieldset'])) { $fieldset = $options['fieldset']; + unset($options['fieldset']); } + if (empty($fields)) { $fields = $modelFields; } @@ -971,11 +975,11 @@ class FormHelper extends AppHelper { $out .= $this->input($name, $options); } - if (is_string($fieldset)) { - $fieldsetClass = sprintf(' class="%s"', $fieldset); - } else { - $fieldsetClass = ''; - } + if (is_string($fieldset)) { + $fieldsetClass = array('class' => $fieldset); + } else { + $fieldsetClass = ''; + } if ($fieldset) { if ($legend) { @@ -1545,9 +1549,9 @@ class FormHelper extends AppHelper { $legend = __(Inflector::humanize($this->field())); } - $fieldset = ''; + $fieldsetAttrs = ''; if (isset($attributes['fieldset'])) { - $fieldset = $attributes['fieldset']; + $fieldsetAttrs = array('class' => $attributes['fieldset']); unset($attributes['fieldset']); } @@ -1644,13 +1648,10 @@ class FormHelper extends AppHelper { if (is_array($between)) { $between = ''; } + if ($legend) { - if (is_string($fieldset)) { - $fieldsetClass = sprintf(' class="%s"', $fieldset); - } else { - $fieldsetClass = ''; - } - $out = $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $between . $out); + $out = $this->Html->useTag('legend', $legend) . $between . $out; + $out = $this->Html->useTag('fieldset', $fieldsetAttrs, $out); } return $out; }