Permalink
Browse files

Fix incorrect radio selection with falsey values.

Use strval() to work around 0 == '' type issues.
Cleanup some tests.

Fixes #3221
  • Loading branch information...
markstory committed Sep 19, 2012
1 parent da6d49e commit 0f0b5e7668e9cf8160efea26b9f0e84b35fa3262
Showing with 57 additions and 78 deletions.
  1. +56 −77 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +1 −1 lib/Cake/View/Helper/FormHelper.php
@@ -3183,80 +3183,6 @@ public function testRadio() {
);
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
array('label' => array('for' => 'ModelField1')),
'Yes',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
array('label' => array('for' => 'ModelField0')),
'No',
'/label',
'/fieldset'
);
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'Yes',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
array('label' => array('for' => 'ModelField0')),
'No',
'/label',
'/fieldset'
);
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'Yes',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
array('label' => array('for' => 'ModelField0')),
'No',
'/label',
'/fieldset'
);
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'Yes',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
array('label' => array('for' => 'ModelField0')),
'No',
'/label',
'/fieldset'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
$expected = array(
'div' => array('class' => 'input radio'),
@@ -3445,6 +3371,59 @@ public function testRadio() {
$this->assertTags($result, $expected);
}
/**
* Test that radios with a 0 value are selected under the correct conditions.
*
* @return void
*/
public function testRadioOptionWithZeroValue() {
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'Yes',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
array('label' => array('for' => 'ModelField0')),
'No',
'/label',
'/fieldset'
);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
$this->assertTags($result, $expected);
$expected = array(
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'Yes',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
array('label' => array('for' => 'ModelField0')),
'No',
'/label',
'/fieldset'
);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
$this->assertTags($result, $expected);
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
$this->assertTags($result, $expected);
}
/**
* test disabled radio options
*
@@ -3454,7 +3433,7 @@ public function testRadioDisabled() {
$result = $this->Form->radio(
'Model.field',
array('option A', 'option B'),
array('disabled' => array('option A'), 'value' => 'option A')
array('disabled' => array('option A'), 'value' => '0')
);
$expected = array(
'fieldset' => array(),
@@ -3476,7 +3455,7 @@ public function testRadioDisabled() {
$result = $this->Form->radio(
'Model.field',
array('option A', 'option B'),
array('disabled' => true, 'value' => 'option A')
array('disabled' => true, 'value' => '0')
);
$expected = array(
'fieldset' => array(),
@@ -3498,7 +3477,7 @@ public function testRadioDisabled() {
$result = $this->Form->radio(
'Model.field',
array('option A', 'option B'),
array('disabled' => 'disabled', 'value' => 'option A')
array('disabled' => 'disabled', 'value' => '0')
);
$expected = array(
'fieldset' => array(),
@@ -1377,7 +1377,7 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
foreach ($options as $optValue => $optTitle) {
$optionsHere = array('value' => $optValue);
if (isset($value) && $optValue == $value) {
if (isset($value) && strval($optValue) === strval($value)) {
$optionsHere['checked'] = 'checked';
}
if ($disabled && (!is_array($disabled) || in_array($optValue, $disabled))) {

0 comments on commit 0f0b5e7

Please sign in to comment.