Skip to content

Commit

Permalink
Fix inability to override templates through FormHelper::input() options.
Browse files Browse the repository at this point in the history
Closes #113
  • Loading branch information
ADmad committed Jan 24, 2016
1 parent 0c82f70 commit c17e319
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ public function input($fieldName, array $options = [])
'templateVars' => []
];
$options = $this->_parseOptions($fieldName, $options);
$reset = $this->templates();

$newTemplates = $options['templates'];
if ($newTemplates) {
$this->templater()->push();
$templateMethod = is_string($options['templates']) ? 'load' : 'add';
$this->templater()->{$templateMethod}($options['templates']);
$options['templates'] = [];
}

switch ($options['type']) {
case 'checkbox':
Expand All @@ -203,22 +210,16 @@ public function input($fieldName, array $options = [])
break;
case 'radio':
$isInline = (isset($options['inline']) && $options['inline'] === true);

$templates = [];
if ($isInline && $this->_align !== 'horizontal') {
$templates['formGroup'] = $this->templater()->get('radioInlineFormGroup');
$options['templates']['formGroup'] = $this->templater()->get('radioInlineFormGroup');
}
if (!$isInline) {
$templates['nestingLabel'] = $this->templater()->get('radioNestingLabel');
$options['templates']['nestingLabel'] = $this->templater()->get('radioNestingLabel');
}

$this->templater()->add($templates);
break;
case 'select':
if (isset($options['multiple']) && $options['multiple'] === 'checkbox') {
$this->templater()->add([
'checkboxWrapper' => $this->templater()->get('multipleCheckboxWrapper')
]);
$options['templates']['checkboxWrapper'] = $this->templater()->get('multipleCheckboxWrapper');
$options['type'] = 'multicheckbox';
}
break;
Expand All @@ -243,7 +244,9 @@ public function input($fieldName, array $options = [])
}

$result = parent::input($fieldName, $options);
$this->templates($reset);
if ($newTemplates) {
$this->templater()->pop();
}
return $result;
}

Expand Down
82 changes: 82 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,62 @@ public function testInlineRadioInputHorizontal()
$this->assertHtml($expected, $result);
}

/**
* https://github.com/FriendsOfCake/bootstrap-ui/pull/113
*
* @return void
*/
public function testRadioInputCustomTemplate()
{
$templates = [
'radioNestingLabel' => '<div class="radio custom-class">{{hidden}}<label{{attrs}}>{{input}}{{text}}</label></div>',
];

$this->Form->create($this->article);

$result = $this->Form->input('published', [
'type' => 'radio',
'options' => ['Yes', 'No'],
'templates' => $templates
]);
$expected = [
['div' => ['class' => 'form-group radio']],
['label' => true],
'Published',
'/label',
['input' => [
'type' => 'hidden',
'name' => 'published',
'value' => '',
]],
['div' => ['class' => 'radio custom-class']],
['label' => ['for' => 'published-0']],
['input' => [
'type' => 'radio',
'name' => 'published',
'value' => 0,
'id' => 'published-0',
]],
'Yes',
'/label',
'/div',
['div' => ['class' => 'radio custom-class']],
['label' => ['for' => 'published-1']],
['input' => [
'type' => 'radio',
'name' => 'published',
'value' => 1,
'id' => 'published-1',
]],
'No',
'/label',
'/div',
'/div'
];

$this->assertHtml($expected, $result);
}

public function testBasicCheckboxInput()
{
$this->Form->create($this->article);
Expand Down Expand Up @@ -1138,6 +1194,32 @@ public function testHelpText()
'/div'
];
$this->assertHtml($expected, $result);

$result = $this->Form->input('title', [
'help' => 'help text',
'templates' => ['help' => '<div class="custom-help-block">{{content}}</div>']
]);
$expected = [
'div' => ['class' => 'form-group text required has-error'],
'label' => ['class' => 'control-label', 'for' => 'title'],
'Title',
'/label',
'input' => [
'type' => 'text',
'name' => 'title',
'id' => 'title',
'class' => 'form-control ',
'required' => 'required'
],
['div' => ['class' => 'help-block']],
'error message',
'/div',
['div' => ['class' => 'custom-help-block']],
'help text',
'/div',
'/div'
];
$this->assertHtml($expected, $result);
}

public function testHelpTextHorizontal()
Expand Down

0 comments on commit c17e319

Please sign in to comment.