Skip to content

Commit

Permalink
Simplify method and remove param weirdness.
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Apr 9, 2014
1 parent 84c03dc commit 95325a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
20 changes: 4 additions & 16 deletions src/View/Helper/FormHelper.php
Expand Up @@ -712,7 +712,7 @@ public function label($fieldName, $text = null, $options = array()) {
}

/**
* Generate a set of inputs for `$fields`. If $fields is null the fields of current model
* Generate a set of inputs for `$fields`. If $fields is empty the fields of current model
* will be used.
*
* You can customize individual inputs through `$fields`.
Expand All @@ -725,16 +725,12 @@ public function label($fieldName, $text = null, $options = array()) {
* You can exclude fields using the `$blacklist` parameter:
*
* {{{
* $this->Form->inputs(null, ['title']);
* $this->Form->inputs([], ['title']);
* }}}
*
* In the above example, no field would be generated for the title field.
*
* 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.
*
* @param mixed $fields An array of customizations for the fields that will be
* @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 $blacklist A list of fields to not create inputs for.
* @param array $options Options array. Valid keys are:
Expand All @@ -744,20 +740,12 @@ public function label($fieldName, $text = null, $options = array()) {
* @return string Completed form inputs.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
*/
public function inputs($fields = null, array $blacklist = [], array $options = []) {
public function inputs(array $fields = [], array $blacklist = [], array $options = []) {
$fieldset = $legend = true;
$context = $this->_getContext();

$modelFields = $context->fieldNames();

if (is_string($fields)) {
$legend = $fields;
$fields = [];
} elseif (is_bool($fields)) {
$fieldset = $legend = $fields;
$fields = [];
}

$fields = array_merge(
Hash::normalize($modelFields),
Hash::normalize((array)$fields)
Expand Down
14 changes: 5 additions & 9 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -2649,7 +2649,7 @@ public function testInputMagicSelectChangeToRadio() {
*/
public function testFormInputsLegendFieldset() {
$this->Form->create($this->article);
$result = $this->Form->inputs('The Legend');
$result = $this->Form->inputs([], [], array('legend' => 'The Legend'));
$expected = array(
'<fieldset',
'<legend',
Expand All @@ -2659,19 +2659,15 @@ public function testFormInputsLegendFieldset() {
);
$this->assertTags($result, $expected);

$result = $this->Form->inputs(null, array(), array('legend' => 'Field of Dreams', 'fieldset' => true));
$result = $this->Form->inputs([], [], array('fieldset' => true, 'legend' => 'Field of Dreams'));
$this->assertContains('<legend>Field of Dreams</legend>', $result);
$this->assertContains('<fieldset>', $result);

$result = $this->Form->inputs('Field of Dreams', array(), array('fieldset' => true));
$this->assertContains('<legend>Field of Dreams</legend>', $result);
$this->assertContains('<fieldset>', $result);

$result = $this->Form->inputs(null, array(), array('fieldset' => false, 'legend' => false));
$result = $this->Form->inputs([], [], array('fieldset' => false, 'legend' => false));
$this->assertNotContains('<legend>', $result);
$this->assertNotContains('<fieldset>', $result);

$result = $this->Form->inputs(null, array(), array('fieldset' => false, 'legend' => 'Hello'));
$result = $this->Form->inputs([], [], array('fieldset' => false, 'legend' => 'Hello'));
$this->assertNotContains('<legend>', $result);
$this->assertNotContains('<fieldset>', $result);

Expand Down Expand Up @@ -2734,7 +2730,7 @@ public function testFormInputs() {
$this->assertTags($result, $expected);

$this->Form->create($this->article);
$result = $this->Form->inputs('Hello');
$result = $this->Form->inputs([], [], ['legend' => 'Hello']);
$expected = array(
'fieldset' => array(),
'legend' => array(),
Expand Down

0 comments on commit 95325a6

Please sign in to comment.