Skip to content

Commit

Permalink
Add templates option to create().
Browse files Browse the repository at this point in the history
This will make it easier to override templates on a per form basis.
  • Loading branch information
markstory committed Mar 10, 2014
1 parent e54ffbc commit 3100338
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/View/Helper/FormHelper.php
Expand Up @@ -256,6 +256,10 @@ protected function _isRequiredField($validationRules) {
* - `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.
Expand Down Expand Up @@ -284,10 +288,17 @@ public function create($model = null, $options = []) {
'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']);

Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -337,6 +337,28 @@ public function testCreateGet() {
$this->assertTags($result, $expected);
}

/**
* 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
*
Expand Down

0 comments on commit 3100338

Please sign in to comment.