Skip to content

Commit

Permalink
Merge pull request #6115 from HavokInspiration/3.0-4295
Browse files Browse the repository at this point in the history
3.0 - Gives the ability to fieldset to have html attributes
  • Loading branch information
markstory committed Mar 20, 2015
2 parents d5e3aea + 5722c37 commit a3ee837
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/View/Helper/FormHelper.php
Expand Up @@ -97,7 +97,7 @@ class FormHelper extends Helper
'errorList' => '<ul>{{content}}</ul>',
'errorItem' => '<li>{{text}}</li>',
'file' => '<input type="file" name="{{name}}"{{attrs}}>',
'fieldset' => '<fieldset>{{content}}</fieldset>',
'fieldset' => '<fieldset{{attrs}}>{{content}}</fieldset>',
'formStart' => '<form{{attrs}}>',
'formEnd' => '</form>',
'formGroup' => '{{label}}{{input}}',
Expand Down Expand Up @@ -836,7 +836,9 @@ public function label($fieldName, $text = null, array $options = [])
* @param array $fields An array of customizations for the fields that will be
* generated. This array allows you to set custom types, labels, or other options.
* @param array $options Options array. Valid keys are:
* - `fieldset` Set to false to disable the fieldset.
* - `fieldset` Set to false to disable the fieldset. You can also pass an array of params to be
* applied as HTML attributes to the fieldset tag. If you pass an empty array, the fieldset will
* be enabled
* - `legend` Set to false to disable the legend for the generated input set. Or supply a string
* to customize the legend text.
* @return string Completed form inputs.
Expand Down Expand Up @@ -870,7 +872,9 @@ public function allInputs(array $fields = [], array $options = [])
* @param array $fields An array of the fields to generate. This array allows you to set custom
* types, labels, or other options.
* @param array $options Options array. Valid keys are:
* - `fieldset` Set to false to disable the fieldset.
* - `fieldset` Set to false to disable the fieldset. You can also pass an array of params to be
* applied as HTML attributes to the fieldset tag. If you pass an empty array, the fieldset will
* be enabled
* - `legend` Set to false to disable the legend for the generated input set. Or supply a string
* to customize the legend text.
* @return string Completed form inputs.
Expand All @@ -897,7 +901,9 @@ public function inputs(array $fields, array $options = [])
*
* @param string $fields the form inputs to wrap in a fieldset
* @param array $options Options array. Valid keys are:
* - `fieldset` Set to false to disable the fieldset.
* - `fieldset` Set to false to disable the fieldset. You can also pass an array of params to be
* applied as HTML attributes to the fieldset tag. If you pass an empty array, the fieldset will
* be enabled
* - `legend` Set to false to disable the legend for the generated input set. Or supply a string
* to customize the legend text.
* @return string Completed form inputs.
Expand Down Expand Up @@ -925,11 +931,16 @@ public function fieldset($fields = '', array $options = [])
$legend = sprintf($actionName, $modelName);
}

if ($fieldset) {
if ($fieldset !== false) {
if ($legend) {
$out = $this->formatTemplate('legend', ['text' => $legend]) . $out;
}
$out = $this->formatTemplate('fieldset', ['content' => $out]);

$fieldsetParams = ['content' => $out, 'attrs' => ''];
if (is_array($fieldset) && !empty($fieldset)) {
$fieldsetParams['attrs'] = $this->templater()->formatAttributes($fieldset);
}
$out = $this->formatTemplate('fieldset', $fieldsetParams);
}
return $out;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -3117,6 +3117,35 @@ public function testFormInputsLegendFieldset()
'*/fieldset',
];
$this->assertHtml($expected, $result);

$this->Form->create($this->article);
$result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
$expected = [
'<fieldset',
'<legend',
'The Legend',
'/legend',
'*/fieldset',
];
$this->assertHtml($expected, $result);

$this->Form->create($this->article);
$result = $this->Form->allInputs([], [
'fieldset' => [
'class' => 'some-class some-other-class',
'disabled' => true,
'data-param' => 'a-param'
],
'legend' => 'The Legend'
]);
$expected = [
'<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
'<legend',
'The Legend',
'/legend',
'*/fieldset',
];
$this->assertHtml($expected, $result);
}

/**
Expand Down

0 comments on commit a3ee837

Please sign in to comment.