Skip to content

Commit

Permalink
Don't set "required" attribute for checkboxes (unless explicitly spec…
Browse files Browse the repository at this point in the history
…ified).

Adding it prevents user from submitting form with checkbox unchecked when the "boolean" validation rule is specified for the field.
Closes #3560
  • Loading branch information
ADmad committed Jan 23, 2013
1 parent 51946ff commit 82f34c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Contact extends CakeTestModel {
'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
),
'boolean_field' => array('rule' => 'boolean')
);

/**
Expand Down Expand Up @@ -7279,6 +7280,28 @@ public function testFormInputRequiredDetection() {
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
$expected = array(
'div' => array('class' => 'input checkbox required'),
array('input' => array(
'type' => 'hidden',
'name' => 'data[Contact][boolean_field]',
'id' => 'ContactBooleanField_',
'value' => '0'
)),
array('input' => array(
'type' => 'checkbox',
'name' => 'data[Contact][boolean_field]',
'value' => '1',
'id' => 'ContactBooleanField'
)),
'label' => array('for' => 'ContactBooleanField'),
'Boolean Field',
'/label',
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Cake/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1379,9 +1379,10 @@ public function checkbox($fieldName, $options = array()) {
unset($options['default']);
}

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

if (empty($options['value'])) {
$options['value'] = 1;
Expand Down

0 comments on commit 82f34c4

Please sign in to comment.