Permalink
Browse files

Fix bad radio and checkboxes values binding in case of booleans

  • Loading branch information...
1 parent 7f6da57 commit fcd570c44b2cc85ab627c6eb41ed9319252c354b @mehlah mehlah committed Dec 31, 2012
Showing with 50 additions and 8 deletions.
  1. +4 −4 template/helper/Form.php
  2. +46 −4 tests/cases/template/helper/FormTest.php
View
@@ -645,8 +645,8 @@ public function checkbox($name, array $options = array()) {
list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
list($scope, $options) = $this->_options($defaults, $options);
- if (!isset($options['checked']) && ($bound = $this->binding($key)->data)) {
- $options['checked'] = ($bound == $default);
+ if (!isset($options['checked'])) {
+ $options['checked'] = ($this->binding($key)->data == $default);
}
if ($scope['hidden']) {
$out = $this->hidden($name, array('value' => '', 'id' => false));
@@ -674,8 +674,8 @@ public function radio($name, array $options = array()) {
list($name, $options, $template) = $this->_defaults(__FUNCTION__, $name, $options);
list($scope, $options) = $this->_options($defaults, $options);
- if (!isset($options['checked']) && ($bound = $this->binding($name)->data)) {
- $options['checked'] = ($bound == $default);
+ if (!isset($options['checked'])) {
+ $options['checked'] = ($this->binding($name)->data == $default);
}
$options['value'] = $scope['value'];
@@ -396,8 +396,19 @@ public function testCheckboxGeneration() {
$this->assertTags($result, array(
array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')),
array('input' => array(
- 'type' => 'checkbox', 'value' => '1', 'name' => 'foo',
- 'checked' => 'checked', 'id' => 'MockFormPostFoo'
+ 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo',
+ 'checked' => 'checked'
+ ))
+ ));
+
+ $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false)));
+ $this->form->create($record);
+
+ $result = $this->form->checkbox('foo');
+ $this->assertTags($result, array(
+ array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')),
+ array('input' => array(
+ 'type' => 'checkbox', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo'
))
));
@@ -497,6 +508,17 @@ public function testCustomValueCheckbox() {
'type' => 'checkbox', 'value' => 'nose', 'name' => 'foo', 'id' => 'MockFormPostFoo'
))
));
+
+ $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false)));
+ $this->form->create($record);
+ $result = $this->form->checkbox('foo', array('value' => '0'));
+ $this->assertTags($result, array(
+ array('input' => array('type' => 'hidden', 'value' => '', 'name' => 'foo')),
+ array('input' => array(
+ 'type' => 'checkbox', 'value' => '0', 'name' => 'foo', 'id' => 'MockFormPostFoo',
+ 'checked' => 'checked'
+ ))
+ ));
}
public function testRadioGeneration() {
@@ -527,8 +549,18 @@ public function testRadioGeneration() {
$result = $this->form->radio('foo');
$this->assertTags($result, array(
array('input' => array(
- 'type' => 'radio', 'value' => '1', 'name' => 'foo',
- 'checked' => 'checked', 'id' => 'MockFormPostFoo'
+ 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo',
+ 'checked' => 'checked'
+ ))
+ ));
+
+ $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false)));
+ $this->form->create($record);
+
+ $result = $this->form->radio('foo');
+ $this->assertTags($result, array(
+ array('input' => array(
+ 'type' => 'radio', 'value' => '1', 'name' => 'foo', 'id' => 'MockFormPostFoo'
))
));
}
@@ -605,6 +637,16 @@ public function testCustomValueRadio() {
'type' => 'checkbox', 'value' => 'nose', 'name' => 'foo', 'id' => 'MockFormPostFoo'
))
));
+
+ $record = new Record(array('model' => $this->_model, 'data' => array('foo' => false)));
+ $this->form->create($record);
+ $result = $this->form->radio('foo', array('value' => '0'));
+ $this->assertTags($result, array(
+ array('input' => array(
+ 'type' => 'radio', 'value' => '0', 'name' => 'foo',
+ 'id' => 'MockFormPostFoo', 'checked' => 'checked'
+ ))
+ ));
}
public function testSelectGeneration() {

0 comments on commit fcd570c

Please sign in to comment.