Permalink
Browse files

Allow empty values in checkboxes.

Allow the checkbox value attribute to be empty. This is required to make
checkboxes with a value of 0.

Fixes #2717
  • Loading branch information...
1 parent b73cbf4 commit 605351d0c9706e33bde9b0354a5cf494d65d333b @markstory markstory committed Jan 31, 2014
Showing with 30 additions and 4 deletions.
  1. +29 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +1 −4 lib/Cake/View/Helper/FormHelper.php
@@ -5837,6 +5837,35 @@ public function testCheckboxHiddenField() {
}
/**
+ * 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
*
* Test generation of date/time select elements
@@ -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'])

0 comments on commit 605351d

Please sign in to comment.