Skip to content

Commit

Permalink
Allow empty values in checkboxes.
Browse files Browse the repository at this point in the history
Allow the checkbox value attribute to be empty. This is required to make
checkboxes with a value of 0.

Fixes #2717
  • Loading branch information
markstory committed Jan 31, 2014
1 parent b73cbf4 commit 605351d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 29 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -5836,6 +5836,35 @@ public function testCheckboxHiddenField() {
$this->assertTags($result, $expected);
}

/**
* Test that a checkbox can have 0 for the value and 1 for the hidden input.
*
* @return void
*/
public function testCheckboxZeroValue() {
$result = $this->Form->input('User.get_spam', array(
'type' => 'checkbox',
'value' => '0',
'hiddenField' => '1',
));
$expected = array(
'div' => array('class' => 'input checkbox'),
array('input' => array(
'type' => 'hidden', 'name' => 'data[User][get_spam]',
'value' => '1', 'id' => 'UserGetSpam_'
)),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[User][get_spam]',
'value' => '0', 'id' => 'UserGetSpam'
)),
'label' => array('for' => 'UserGetSpam'),
'Get Spam',
'/label',
'/div'
);
$this->assertTags($result, $expected);
}

/**
* testDateTime method
*
Expand Down
5 changes: 1 addition & 4 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1402,14 +1402,11 @@ public function checkbox($fieldName, $options = array()) {
unset($options['default']);
}

$options += array('required' => false);
$options += array('value' => 1, 'required' => false);
$options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
$value = current($this->value($valueOptions));
$output = '';

if (empty($options['value'])) {
$options['value'] = 1;
}
if (
(!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
!empty($options['checked'])
Expand Down

0 comments on commit 605351d

Please sign in to comment.