Skip to content

Commit

Permalink
Making input() tests pass when rendering checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 23, 2014
1 parent 104cef5 commit 82ec578
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/View/Helper/FormHelper.php
Expand Up @@ -165,6 +165,7 @@ class FormHelper extends Helper {
'radioContainer' => '{{input}}{{label}}',
'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
'formGroup' => '{{label}}{{input}}',
'checkboxFormGroup' => '{{input}}{{label}}',
'groupContainer' => '<div class="input {{type}}{{required}}">{{content}}</div>',
'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>'
];
Expand Down Expand Up @@ -881,8 +882,9 @@ public function input($fieldName, $options = array()) {
unset($options['error']);
}

$groupTemplate = $options['type'] === 'checkbox' ? 'checkboxFormGroup' : 'formGroup';
$input = $this->{$options['type']}($fieldName, $options);
$result = $this->formatTemplate('formGroup', compact('input', 'label'));
$result = $this->formatTemplate($groupTemplate, compact('input', 'label'));

if ($options['type'] !== 'hidden') {
$result = $this->formatTemplate($template, [
Expand Down Expand Up @@ -1167,7 +1169,7 @@ public function checkbox($fieldName, $options = []) {
}
$output = $this->hidden($fieldName, $hiddenOptions);
}
unset($options['hiddenField']);
unset($options['hiddenField'], $options['type']);
return $output . $this->widget('checkbox', $options);
}

Expand Down
21 changes: 10 additions & 11 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -2683,30 +2683,29 @@ public function testInputZero() {
* @return void
*/
public function testInputCheckbox() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
'/div'
);
$this->assertTags($result, $expected);
Expand All @@ -2718,15 +2717,15 @@ public function testInputCheckbox() {
));
$expected = array(
'div' => array('class' => 'input checkbox'),
'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
array('input' => array(
'type' => 'checkbox',
'name' => 'data[User][disabled]',
'name' => 'User[disabled]',
'value' => '1',
'id' => 'UserDisabled',
'id' => 'user-disabled',
'data-foo' => 'disabled'
)),
'label' => array('for' => 'UserDisabled'),
'label' => array('for' => 'user-disabled'),
'Disabled',
'/label',
'/div'
Expand Down

0 comments on commit 82ec578

Please sign in to comment.