Browse Source

split up the inputs function

Seperate the existing "give me all inputs for the current model" logic
from the "give me a fieldset, with these fields in it" logic.
AD7six 12 years ago
parent
commit
c2f8abf64f
2 changed files with 69 additions and 29 deletions
  1. 58 18
      src/View/Helper/FormHelper.php
  2. 11 11
      tests/TestCase/View/Helper/FormHelperTest.php

+ 58 - 18
src/View/Helper/FormHelper.php

@@ -729,7 +729,7 @@ class FormHelper extends Helper {
  *
  * You can customize individual inputs through `$fields`.
  * {{{
- * $this->Form->inputs([
+ * $this->Form->allInputs([
  *   'name' => ['label' => 'custom label']
  * ]);
  * }}}
@@ -737,7 +737,7 @@ class FormHelper extends Helper {
  * You can exclude fields by specifying them as false:
  *
  * {{{
- * $this->Form->inputs(['title' => false]);
+ * $this->Form->allInputs(['title' => false]);
  * }}}
  *
  * In the above example, no field would be generated for the title field.
@@ -751,8 +751,7 @@ class FormHelper extends Helper {
  * @return string Completed form inputs.
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
  */
-	public function inputs(array $fields = [], array $options = []) {
-		$fieldset = $legend = true;
+	public function allInputs(array $fields = [], array $options = []) {
 		$context = $this->_getContext();
 
 		$modelFields = $context->fieldNames();
@@ -762,6 +761,61 @@ class FormHelper extends Helper {
 			Hash::normalize($fields)
 		);
 
+		return $this->inputs($fields, $options);
+	}
+
+/**
+ * Generate a set of inputs for `$fields`
+ *
+ * You can customize individual inputs through `$fields`.
+ * {{{
+ * $this->Form->inputs([
+ *   'name' => ['label' => 'custom label']
+ * ]);
+ * }}}
+ *
+ * In the above example, no field would be generated for the title field.
+ *
+ * @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.
+ * - `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.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
+ */
+	public function inputs(array $fields, array $options = []) {
+		$fields = Hash::normalize($fields);
+
+		$out = '';
+		foreach ($fields as $name => $opts) {
+			if ($opts === false) {
+				continue;
+			}
+
+			$out .= $this->input($name, (array)$opts);
+		}
+
+		return $this->fieldset($out, $options);
+	}
+
+/**
+ * Wrap a set of inputs in a fieldset
+ *
+ * @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.
+ * - `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.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
+ */
+	public function fieldset($fields = '', array $options = []) {
+		$fieldset = $legend = true;
+		$context = $this->_getContext();
+		$out = $fields;
+
 		if (isset($options['legend'])) {
 			$legend = $options['legend'];
 		}
@@ -779,20 +833,6 @@ class FormHelper extends Helper {
 			$legend = sprintf($actionName, $modelName);
 		}
 
-		$out = null;
-		foreach ($fields as $name => $options) {
-			if ($options === false) {
-				continue;
-			}
-
-			if (is_numeric($name) && !is_array($options)) {
-				$name = $options;
-				$options = [];
-			}
-			$entity = explode('.', $name);
-			$out .= $this->input($name, (array)$options);
-		}
-
 		if ($fieldset) {
 			if ($legend) {
 				$out = $this->formatTemplate('legend', ['text' => $legend]) . $out;

+ 11 - 11
tests/TestCase/View/Helper/FormHelperTest.php

@@ -2679,7 +2679,7 @@ class FormHelperTest extends TestCase {
  */
 	public function testFormInputsLegendFieldset() {
 		$this->Form->create($this->article);
-		$result = $this->Form->inputs([], array('legend' => 'The Legend'));
+		$result = $this->Form->allInputs([], array('legend' => 'The Legend'));
 		$expected = array(
 			'<fieldset',
 			'<legend',
@@ -2689,15 +2689,15 @@ class FormHelperTest extends TestCase {
 		);
 		$this->assertTags($result, $expected);
 
-		$result = $this->Form->inputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
+		$result = $this->Form->allInputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
 		$this->assertContains('<legend>Field of Dreams</legend>', $result);
 		$this->assertContains('<fieldset>', $result);
 
-		$result = $this->Form->inputs([], array('fieldset' => false, 'legend' => false));
+		$result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => false));
 		$this->assertNotContains('<legend>', $result);
 		$this->assertNotContains('<fieldset>', $result);
 
-		$result = $this->Form->inputs([], array('fieldset' => false, 'legend' => 'Hello'));
+		$result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => 'Hello'));
 		$this->assertNotContains('<legend>', $result);
 		$this->assertNotContains('<fieldset>', $result);
 
@@ -2705,7 +2705,7 @@ class FormHelperTest extends TestCase {
 		$this->Form->request->params['prefix'] = 'admin';
 		$this->Form->request->params['action'] = 'admin_edit';
 		$this->Form->request->params['controller'] = 'articles';
-		$result = $this->Form->inputs();
+		$result = $this->Form->allInputs();
 		$expected = [
 			'<fieldset',
 			'<legend',
@@ -2723,7 +2723,7 @@ class FormHelperTest extends TestCase {
  */
 	public function testFormInputs() {
 		$this->Form->create($this->article);
-		$result = $this->Form->inputs();
+		$result = $this->Form->allInputs();
 		$expected = array(
 			'<fieldset',
 			'<legend', 'New Article', '/legend',
@@ -2740,7 +2740,7 @@ class FormHelperTest extends TestCase {
 		);
 		$this->assertTags($result, $expected);
 
-		$result = $this->Form->inputs([
+		$result = $this->Form->allInputs([
 			'published' => ['type' => 'boolean']
 		]);
 		$expected = array(
@@ -2760,7 +2760,7 @@ class FormHelperTest extends TestCase {
 		$this->assertTags($result, $expected);
 
 		$this->Form->create($this->article);
-		$result = $this->Form->inputs([], ['legend' => 'Hello']);
+		$result = $this->Form->allInputs([], ['legend' => 'Hello']);
 		$expected = array(
 			'fieldset' => array(),
 			'legend' => array(),
@@ -2790,7 +2790,7 @@ class FormHelperTest extends TestCase {
 			'*/div',
 			'/fieldset'
 		);
-		$result = $this->Form->inputs(
+		$result = $this->Form->allInputs(
 			array('foo' => array('type' => 'text')),
 			array('legend' => false)
 		);
@@ -2804,7 +2804,7 @@ class FormHelperTest extends TestCase {
  */
 	public function testFormInputsBlacklist() {
 		$this->Form->create($this->article);
-		$result = $this->Form->inputs([
+		$result = $this->Form->allInputs([
 			'id' => false
 		]);
 		$expected = array(
@@ -2823,7 +2823,7 @@ class FormHelperTest extends TestCase {
 		$this->assertTags($result, $expected);
 
 		$this->Form->create($this->article);
-		$result = $this->Form->inputs([
+		$result = $this->Form->allInputs([
 			'id' => []
 		]);
 		$expected = array(