Browse Source

Add conditional container templates.

Having conditionally used container templates lets developers define
different container HTML for each input type.

Refs #3651
mark_story 11 years ago
parent
commit
61c41758f9
2 changed files with 49 additions and 1 deletions
  1. 5 1
      src/View/Helper/FormHelper.php
  2. 44 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 5 - 1
src/View/Helper/FormHelper.php

@@ -902,7 +902,11 @@ class FormHelper extends Helper {
 			$errorSuffix = empty($error) ? '' : 'Error';
 			unset($options['error']);
 		}
-		$template = 'inputContainer' . $errorSuffix;
+
+		$template = $options['type'] . 'Container' . $errorSuffix;
+		if (!$this->templates($template)) {
+			$template = 'inputContainer' . $errorSuffix;
+		}
 
 		$label = $options['label'];
 		if ($options['type'] !== 'radio') {

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

@@ -6068,6 +6068,50 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test that *Container templates are used by input.
+ *
+ * @return void
+ */
+	public function testInputContainerTemplates() {
+		$this->Form->templates([
+			'checkboxContainer' => '<div class="check">{{content}}</div>',
+			'radioContainer' => '<div class="rad">{{content}}</div>',
+			'radioContainerError' => '<div class="rad err">{{content}}</div>',
+		]);
+
+		$this->article['errors'] = [
+			'Article' => ['published' => 'error message']
+		];
+		$this->Form->create($this->article);
+
+		$result = $this->Form->input('accept', [
+			'type' => 'checkbox'
+		]);
+		$expected = [
+			'div' => ['class' => 'check'],
+			['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
+			['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
+			'label' => ['for' => 'accept'],
+			'Accept',
+			'/label',
+			'/div'
+		];
+		$this->assertTags($result, $expected);
+
+		$result = $this->Form->input('accept', [
+			'type' => 'radio',
+			'options' => ['Y', 'N']
+		]);
+		$this->assertContains('<div class="rad">', $result);
+
+		$result = $this->Form->input('Article.published', [
+			'type' => 'radio',
+			'options' => ['Y', 'N']
+		]);
+		$this->assertContains('<div class="rad err">', $result);
+	}
+
+/**
  * Test resetting templates.
  *
  * @return void