Skip to content

Commit

Permalink
Merge pull request #383 from shama/patch-checkbox-hiddenField
Browse files Browse the repository at this point in the history
Ability to set hiddenField value with FormHelper::checkbox
  • Loading branch information
lorenzo committed Dec 19, 2011
2 parents 2d68e7d + 5225fe2 commit 99e9f62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
34 changes: 28 additions & 6 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -4512,16 +4512,16 @@ public function testCheckboxDisabling() {
}

/**
* Test that the hidden input for checkboxes can be removed/omitted from the output.
* Test that the hidden input for checkboxes can be omitted or set to a
* specific value.
*
* @return void
*/
public function testCheckboxHiddenFieldOmission() {
public function testCheckboxHiddenField() {
$result = $this->Form->input('UserForm.something', array(
'type' => 'checkbox',
'hiddenField' => false
)
);
'type' => 'checkbox',
'hiddenField' => false
));
$expected = array(
'div' => array('class' => 'input checkbox'),
array('input' => array(
Expand All @@ -4534,6 +4534,28 @@ public function testCheckboxHiddenFieldOmission() {
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('UserForm.something', array(
'type' => 'checkbox',
'value' => 'Y',
'hiddenField' => 'N',
));
$expected = array(
'div' => array('class' => 'input checkbox'),
array('input' => array(
'type' => 'hidden', 'name' => 'data[UserForm][something]',
'value' => 'N', 'id' => 'UserFormSomething_'
)),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[UserForm][something]',
'value' => 'Y', 'id' => 'UserFormSomething'
)),
'label' => array('for' => 'UserFormSomething'),
'Something',
'/label',
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1232,8 +1232,10 @@ public function checkbox($fieldName, $options = array()) {
}
if ($options['hiddenField']) {
$hiddenOptions = array(
'id' => $options['id'] . '_', 'name' => $options['name'],
'value' => '0', 'secure' => false
'id' => $options['id'] . '_',
'name' => $options['name'],
'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
'secure' => false
);
if (isset($options['disabled']) && $options['disabled'] == true) {
$hiddenOptions['disabled'] = 'disabled';
Expand Down

0 comments on commit 99e9f62

Please sign in to comment.