Skip to content

Commit

Permalink
Fix derpness of fieldset and legend options being mashed inside $…
Browse files Browse the repository at this point in the history
…fields param array.
  • Loading branch information
ADmad committed Nov 15, 2012
1 parent a7f192c commit f7e66da
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
18 changes: 18 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2845,6 +2845,12 @@ public function testFormInputs() {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);


$result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
$this->assertTags($result, $expected);

$result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
$this->assertTags($result, $expected);

$this->Form->create('Contact'); $this->Form->create('Contact');
$this->Form->request['prefix'] = 'admin'; $this->Form->request['prefix'] = 'admin';
$this->Form->request['action'] = 'admin_edit'; $this->Form->request['action'] = 'admin_edit';
Expand Down Expand Up @@ -2908,6 +2914,10 @@ public function testFormInputs() {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);


$this->Form->create('Contact');
$result = $this->Form->inputs(null, null, array('fieldset' => false));
$this->assertTags($result, $expected);

$this->Form->create('Contact'); $this->Form->create('Contact');
$result = $this->Form->inputs(array('fieldset' => true, 'legend' => false)); $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
$expected = array( $expected = array(
Expand Down Expand Up @@ -2960,6 +2970,10 @@ public function testFormInputs() {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);


$this->Form->create('Contact');
$result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
$this->assertTags($result, $expected);

$this->Form->create('Contact'); $this->Form->create('Contact');
$result = $this->Form->inputs('Hello'); $result = $this->Form->inputs('Hello');
$expected = array( $expected = array(
Expand Down Expand Up @@ -3019,6 +3033,10 @@ public function testFormInputs() {
'/fieldset' '/fieldset'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);

$this->Form->create('Contact');
$result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
$this->assertTags($result, $expected);
} }


/** /**
Expand Down
50 changes: 29 additions & 21 deletions lib/Cake/View/Helper/FormHelper.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -805,42 +805,43 @@ public function label($fieldName = null, $text = null, $options = array()) {
} }


/** /**
* Generate a set of inputs for `$fields`. If $fields is null the current model * Generate a set of inputs for `$fields`. If $fields is null the fields of current model
* will be used. * will be used.
* *
* In addition to controller fields output, `$fields` can be used to control legend * You can customize individual inputs through `$fields`.
* and fieldset rendering with the `fieldset` and `legend` keys.
* `$form->inputs(array('legend' => 'My legend'));` Would generate an input set with
* a custom legend. You can customize individual inputs through `$fields` as well.
*
* {{{ * {{{
* $form->inputs(array( * $this->Form->inputs(array(
* 'name' => array('label' => 'custom label') * 'name' => array('label' => 'custom label')
* )); * ));
* }}} * }}}
* *
* In addition to fields control, inputs() allows you to use a few additional options. * In addition to controller fields output, `$fields` can be used to control legend
* and fieldset rendering.
* `$this->Form->inputs('My legend');` Would generate an input set with a custom legend.
* Passing `fieldset` and `legend` key in `$fields` array has been deprecated since 2.3,
* for more fine grained control use the `fieldset` and `legend` keys in `$options` param.
* *
* @param array $fields An array of fields to generate inputs for, or null.
* @param array $blacklist A simple array of fields to not create inputs for.
* @param array $options Options array. Valid keys are:
* - `fieldset` Set to false to disable the fieldset. If a string is supplied it will be used as * - `fieldset` Set to false to disable the fieldset. If a string is supplied it will be used as
* the classname for the fieldset element. * the classname for the fieldset element.
* - `legend` Set to false to disable the legend for the generated input set. Or supply a string * - `legend` Set to false to disable the legend for the generated input set. Or supply a string
* to customize the legend text. * to customize the legend text.
*
* @param array $fields An array of fields to generate inputs for, or null.
* @param array $blacklist a simple array of fields to not create inputs for.
* @return string Completed form inputs. * @return string Completed form inputs.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
*/ */
public function inputs($fields = null, $blacklist = null) { public function inputs($fields = null, $blacklist = null, $options = array()) {
$fieldset = $legend = true; $fieldset = $legend = true;
$model = $this->model(); $model = $this->model();
$modelFields = array_keys($this->_introspectModel($model, 'fields'));
if (is_array($fields)) { if (is_array($fields)) {
if (array_key_exists('legend', $fields)) { if (array_key_exists('legend', $fields) && !in_array('legend', $modelFields)) {
$legend = $fields['legend']; $legend = $fields['legend'];
unset($fields['legend']); unset($fields['legend']);
} }


if (isset($fields['fieldset'])) { if (isset($fields['fieldset']) && !in_array('fieldset', $modelFields)) {
$fieldset = $fields['fieldset']; $fieldset = $fields['fieldset'];
unset($fields['fieldset']); unset($fields['fieldset']);
} }
Expand All @@ -852,8 +853,15 @@ public function inputs($fields = null, $blacklist = null) {
$fields = array(); $fields = array();
} }


if (isset($options['legend'])) {
$legend = $options['legend'];
}
if (isset($options['fieldset'])) {
$fieldset = $options['fieldset'];
}

if (empty($fields)) { if (empty($fields)) {
$fields = array_keys($this->_introspectModel($model, 'fields')); $fields = $modelFields;
} }


if ($legend === true) { if ($legend === true) {
Expand Down Expand Up @@ -892,13 +900,13 @@ public function inputs($fields = null, $blacklist = null) {
$fieldsetClass = ''; $fieldsetClass = '';
} }


if ($fieldset && $legend) { if ($fieldset) {
return $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $out); if ($legend) {
} elseif ($fieldset) { $out = $this->Html->useTag('legend', $legend) . $out;
return $this->Html->useTag('fieldset', $fieldsetClass, $out); }
} else { $out = $this->Html->useTag('fieldset', $fieldsetClass, $out);
return $out;
} }
return $out;
} }


/** /**
Expand Down

0 comments on commit f7e66da

Please sign in to comment.