mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 17:16:18 +00:00
Fix radio() and boolean values.
Boolean values should be cast to integer equivalents, which allows for correct handling of boolean columns and their string equivalents used in form options. Fixes #3512
This commit is contained in:
parent
e132a7c856
commit
3f21d09c1d
2 changed files with 33 additions and 1 deletions
|
@ -3407,10 +3407,11 @@ class FormHelperTest extends CakeTestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that radios with a 0 value are selected under the correct conditions.
|
* Test that radios with a 0 value are selected under the correct conditions.
|
||||||
|
* Also ensure that values that are booleanish are handled correctly.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testRadioOptionWithZeroValue() {
|
public function testRadioOptionWithBooleanishValues() {
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'fieldset' => array(),
|
'fieldset' => array(),
|
||||||
'legend' => array(),
|
'legend' => array(),
|
||||||
|
@ -3432,6 +3433,9 @@ class FormHelperTest extends CakeTestCase {
|
||||||
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
|
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
|
||||||
$this->assertTags($result, $expected);
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'fieldset' => array(),
|
'fieldset' => array(),
|
||||||
'legend' => array(),
|
'legend' => array(),
|
||||||
|
@ -3456,6 +3460,30 @@ class FormHelperTest extends CakeTestCase {
|
||||||
|
|
||||||
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
|
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
|
||||||
$this->assertTags($result, $expected);
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$expected = array(
|
||||||
|
'fieldset' => array(),
|
||||||
|
'legend' => array(),
|
||||||
|
'Field',
|
||||||
|
'/legend',
|
||||||
|
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', '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' => 1));
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1385,6 +1385,10 @@ class FormHelper extends AppHelper {
|
||||||
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
|
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
|
||||||
unset($attributes['hiddenField']);
|
unset($attributes['hiddenField']);
|
||||||
|
|
||||||
|
if (isset($value) && is_bool($value)) {
|
||||||
|
$value = $value ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($options as $optValue => $optTitle) {
|
foreach ($options as $optValue => $optTitle) {
|
||||||
$optionsHere = array('value' => $optValue);
|
$optionsHere = array('value' => $optValue);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue