Browse Source

Merge pull request #3351 from cakephp/dont-use-group-template-for-hidden-fields

Don't use any groupTemplate or generic template for hidden fields
José Lorenzo Rodríguez 12 years ago
parent
commit
49ea25984f
2 changed files with 37 additions and 11 deletions
  1. 13 11
      src/View/Helper/FormHelper.php
  2. 24 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 13 - 11
src/View/Helper/FormHelper.php

@@ -842,19 +842,21 @@ class FormHelper extends Helper {
 		}
 
 		$input = $this->_getInput($fieldName, $options);
-		$label = $this->_getLabel($fieldName, compact('input', 'label') + $options);
+		if ($options['type'] === 'hidden') {
+			$this->templates($originalTemplates);
+			return $input;
+		}
+
+		$label = $this->_getLabel($fieldName, compact('input', 'label', 'error') + $options);
 
 		$groupTemplate = $options['type'] === 'checkbox' ? 'checkboxFormGroup' : 'formGroup';
-		$result = $this->formatTemplate($groupTemplate, compact('input', 'label'));
-
-		if ($options['type'] !== 'hidden') {
-			$result = $this->formatTemplate($template, [
-				'content' => $result,
-				'error' => $error,
-				'required' => $options['required'] ? ' required' : '',
-				'type' => $options['type'],
-			]);
-		}
+		$result = $this->formatTemplate($groupTemplate, compact('input', 'label', 'error'));
+		$result = $this->formatTemplate($template, [
+			'content' => $result,
+			'error' => $error,
+			'required' => $options['required'] ? ' required' : '',
+			'type' => $options['type'],
+		]);
 
 		$this->templates($originalTemplates);
 		return $result;

+ 24 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -2196,6 +2196,30 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test that input() does not create wrapping div and label tag for hidden fields
+ *
+ * @return void
+ */
+	public function testInputHidden() {
+		TableRegistry::get('ValidateUsers', [
+			'className' => __NAMESPACE__ . '\ValidateUsersTable'
+		]);
+		$this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
+
+		$result = $this->Form->input('ValidateUser.id');
+		$expected = array(
+			'input' => array('name', 'type' => 'hidden', 'id')
+		);
+		$this->assertTags($result, $expected);
+
+		$result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
+		$expected = array(
+			'input' => array('name', 'type' => 'hidden', 'id')
+		);
+		$this->assertTags($result, $expected);
+	}
+
+/**
  * test form->input() with datetime
  *
  * @return void