Browse Source

Add templates option to create().

This will make it easier to override templates on a per form basis.
Mark Story 12 years ago
parent
commit
31003389b9
2 changed files with 34 additions and 1 deletions
  1. 12 1
      src/View/Helper/FormHelper.php
  2. 22 0
      tests/TestCase/View/Helper/FormHelperTest.php

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

@@ -256,6 +256,10 @@ class FormHelper extends Helper {
  * - `url` The URL the form submits to. Can be a string or a URL array. If you use 'url'
  *    you should leave 'action' undefined.
  * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
+ * - `templates` The templates you want to use for this form. Any templates will be merged on top of
+ *   the already loaded templates. This option can either be a filename in App/Config that contains
+ *   the templates you want to load, or an array of templates to use. You can use
+ *   resetTemplates() to restore the original templates.
  * - `context` Additional options for the context class. For example the EntityContext accepts a 'table'
  *   option that allows you to set the specific Table class the form should be based on.
  * - `idPrefix` Prefix for generated ID attributes.
@@ -284,10 +288,17 @@ class FormHelper extends Helper {
 			'action' => null,
 			'url' => null,
 			'encoding' => strtolower(Configure::read('App.encoding')),
-			'idPrefix' => null
+			'templates' => null,
+			'idPrefix' => null,
 		];
 
 		$this->_idPrefix = $options['idPrefix'];
+
+		if (!empty($options['templates'])) {
+			$this->templates($options['templates']);
+		}
+		unset($options['templates']);
+
 		$action = $this->url($this->_formUrl($context, $options));
 		unset($options['url'], $options['action'], $options['idPrefix']);
 

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

@@ -338,6 +338,28 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test create() with the templates option.
+ *
+ * @return void
+ */
+	public function testCreateTemplates() {
+		$result = $this->Form->create($this->article, [
+			'templates' => [
+				'formstart' => '<form class="form-horizontal"{{attrs}}>',
+			]
+		]);
+		$expected = [
+			'form' => [
+				'class' => 'form-horizontal',
+				'method' => 'post',
+				'action' => '/articles/add',
+				'accept-charset' => 'utf-8'
+			]
+		];
+		$this->assertTags($result, $expected);
+	}
+
+/**
  * test the create() method
  *
  * @dataProvider requestTypeProvider