Skip to content

Commit

Permalink
Make create() and end() manage the template stack automatically.
Browse files Browse the repository at this point in the history
By having create()/end() manage the templates automatically we can
resolve the issue where templates are left behind after setting per-form
templates.

Refs #3142
Refs #4015
  • Loading branch information
markstory committed Jul 23, 2014
1 parent e85e241 commit 65e633f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/View/Helper/FormHelper.php
Expand Up @@ -260,8 +260,7 @@ protected function _isRequiredField($validationRules) {
* - `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.
* the templates you want to load, or an array of templates to use.
* - `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 @@ -297,10 +296,10 @@ public function create($model = null, $options = []) {
$this->_idPrefix = $options['idPrefix'];
$templater = $this->templater();

if (!empty($options['templates']) && is_array($options['templates'])) {
$templater->add($options['templates']);
} elseif (!empty($options['templates']) && is_string($options['templates'])) {
$templater->load($options['templates']);
if (!empty($options['templates'])) {
$templater->push();
$method = is_string($options['templates']) ? 'load' : 'add';
$templater->{$method}($options['templates']);
}
unset($options['templates']);

Expand Down Expand Up @@ -439,17 +438,17 @@ protected function _csrfField() {
*/
public function end($secureAttributes = []) {
$out = '';

if (
$this->requestType !== 'get' &&
!empty($this->request['_Token'])
) {
$out .= $this->secure($this->fields, $secureAttributes);
$this->fields = array();
}
$templater = $this->templater();
$out .= $templater->format('formend', []);

$out .= $this->formatTemplate('formend', []);

$templater->pop();
$this->requestType = null;
$this->_context = null;
$this->_idPrefix = null;
Expand Down Expand Up @@ -889,11 +888,12 @@ public function input($fieldName, array $options = []) {
$options = $this->_parseOptions($fieldName, $options);
$options += ['id' => $this->_domId($fieldName)];

$originalTemplates = $this->templates();
$templater = $this->templater();
$newTemplates = $options['templates'];

if ($newTemplates) {
$this->templates($options['templates']);
$templater->push();
$templater->add($options['templates']);
}
unset($options['templates']);

Expand All @@ -906,7 +906,7 @@ public function input($fieldName, array $options = []) {
}

$template = $options['type'] . 'Container' . $errorSuffix;
if (!$this->templates($template)) {
if (!$templater->get($template)) {
$template = 'inputContainer' . $errorSuffix;
}

Expand All @@ -917,23 +917,25 @@ public function input($fieldName, array $options = []) {

$input = $this->_getInput($fieldName, $options);
if ($options['type'] === 'hidden') {
$this->templates($originalTemplates);
if ($newTemplates) {
$templater->pop();
}
return $input;
}

$label = $this->_getLabel($fieldName, compact('input', 'label', 'error') + $options);

$groupTemplate = $options['type'] === 'checkbox' ? 'checkboxFormGroup' : 'formGroup';
$result = $this->formatTemplate($groupTemplate, compact('input', 'label', 'error'));
$result = $this->formatTemplate($template, [
$result = $templater->format($groupTemplate, compact('input', 'label', 'error'));
$result = $templater->format($template, [
'content' => $result,
'error' => $error,
'required' => $options['required'] ? ' required' : '',
'type' => $options['type'],
]);

if ($newTemplates) {
$this->templates($originalTemplates);
$templater->pop();
}

return $result;
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -388,6 +388,19 @@ public function testCreateTemplatesFile() {
$this->assertTags($result, $expected);
}

/**
* Test that create() and end() restore templates.
*
* @return void
*/
public function testCreateEndRestoreTemplates() {
$this->Form->create($this->article, [
'templates' => ['input' => 'custom input element']
]);
$this->Form->end();
$this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
}

/**
* test the create() method
*
Expand Down

0 comments on commit 65e633f

Please sign in to comment.