Browse Source

Fix derpness of `fieldset` and `legend` options being mashed inside $fields param array.

ADmad 13 years ago
parent
commit
f7e66da6fc

+ 18 - 0
lib/Cake/Test/Case/View/Helper/FormHelperTest.php

@@ -2845,6 +2845,12 @@ class FormHelperTest extends CakeTestCase {
 		);
 		);
 		$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';
@@ -2909,6 +2915,10 @@ class FormHelperTest extends CakeTestCase {
 		$this->assertTags($result, $expected);
 		$this->assertTags($result, $expected);
 
 
 		$this->Form->create('Contact');
 		$this->Form->create('Contact');
+		$result = $this->Form->inputs(null, null, array('fieldset' => false));
+		$this->assertTags($result, $expected);
+
+		$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(
 			'fieldset' => array(),
 			'fieldset' => array(),
@@ -2961,6 +2971,10 @@ class FormHelperTest extends CakeTestCase {
 		$this->assertTags($result, $expected);
 		$this->assertTags($result, $expected);
 
 
 		$this->Form->create('Contact');
 		$this->Form->create('Contact');
+		$result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
+		$this->assertTags($result, $expected);
+
+		$this->Form->create('Contact');
 		$result = $this->Form->inputs('Hello');
 		$result = $this->Form->inputs('Hello');
 		$expected = array(
 		$expected = array(
 			'fieldset' => array(),
 			'fieldset' => array(),
@@ -3019,6 +3033,10 @@ class FormHelperTest extends CakeTestCase {
 			'/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);
 	}
 	}
 
 
 /**
 /**

+ 29 - 21
lib/Cake/View/Helper/FormHelper.php

@@ -805,42 +805,43 @@ class FormHelper extends AppHelper {
 	}
 	}
 
 
 /**
 /**
- * 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
- * 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.
- *
+ * You can customize individual inputs through `$fields`.
  * {{{
  * {{{
- *	$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']);
 			}
 			}
@@ -852,8 +853,15 @@ class FormHelper extends AppHelper {
 			$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) {
@@ -892,13 +900,13 @@ class FormHelper extends AppHelper {
 			$fieldsetClass = '';
 			$fieldsetClass = '';
 		}
 		}
 
 
-		if ($fieldset && $legend) {
-			return $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $out);
-		} elseif ($fieldset) {
-			return $this->Html->useTag('fieldset', $fieldsetClass, $out);
-		} else {
-			return $out;
+		if ($fieldset) {
+			if ($legend) {
+				$out = $this->Html->useTag('legend', $legend) . $out;
+			}
+			$out = $this->Html->useTag('fieldset', $fieldsetClass, $out);
 		}
 		}
+		return $out;
 	}
 	}
 
 
 /**
 /**