From 82f34c4cb3515e06ca5ab237a9b8e67ad44b601b Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 23 Jan 2013 20:38:24 +0530 Subject: [PATCH] Don't set "required" attribute for checkboxes (unless explicitly specified). Adding it prevents user from submitting form with checkbox unchecked when the "boolean" validation rule is specified for the field. Closes #3560 --- .../Test/Case/View/Helper/FormHelperTest.php | 23 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index b090adcd3..38d43fe06 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -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') ); /** @@ -7279,6 +7280,28 @@ class FormHelperTest extends CakeTestCase { '/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); } /** diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 71b233e40..c823a27ee 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1379,9 +1379,10 @@ class FormHelper extends AppHelper { 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;