Fix disabled elements as array for multiple select and make in_array() work properly here, fix same in_array issues for radio elements and move tests correctly - #1105

This commit is contained in:
euromark 2013-02-07 15:36:00 +01:00
parent 00078e007c
commit d522f412db
2 changed files with 204 additions and 64 deletions

View file

@ -3493,7 +3493,15 @@ class FormHelperTest extends CakeTestCase {
'/fieldset' '/fieldset'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
}
/**
* Test radio inputs with between as string or array. Also ensure
* that an array with less between elements works.
*
* @return void
*/
public function testRadioBetween() {
$result = $this->Form->radio( $result = $this->Form->radio(
'Model.field', 'Model.field',
array('option A', 'option B'), array('option A', 'option B'),
@ -3571,6 +3579,73 @@ class FormHelperTest extends CakeTestCase {
'/fieldset' '/fieldset'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'options' => array('1' => 'first', '2' => 'second'),
'type' => 'radio',
'before' => '--before--',
'after' => '--after--',
'separator' => '--separator--',
'between' => array('--between first--', '--between second--')
));
$expected = array(
'div' => array('class' => 'input radio'),
'--before--',
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'first',
'/label',
'--between first--',
'--separator--',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
array('label' => array('for' => 'ModelField2')),
'second',
'/label',
'--between second--',
'/fieldset',
'--after--',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'options' => array('1' => 'first', '2' => 'second'),
'type' => 'radio',
'before' => '--before--',
'after' => '--after--',
'separator' => '--separator--',
'between' => array('--between first--')
));
$expected = array(
'div' => array('class' => 'input radio'),
'--before--',
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'first',
'/label',
'--between first--',
'--separator--',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
array('label' => array('for' => 'ModelField2')),
'second',
'/label',
'/fieldset',
'--after--',
'/div'
);
$this->assertTags($result, $expected);
} }
/** /**
@ -3663,7 +3738,7 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->radio( $result = $this->Form->radio(
'Model.field', 'Model.field',
array('option A', 'option B'), array('option A', 'option B'),
array('disabled' => array('option A'), 'value' => '0') array('disabled' => array(0), 'value' => '0')
); );
$expected = array( $expected = array(
'fieldset' => array(), 'fieldset' => array(),
@ -3727,17 +3802,13 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array( $result = $this->Form->input('Model.field', array(
'options' => array('1' => 'first', '2' => 'second'), 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
'type' => 'radio', 'type' => 'radio',
'before' => '--before--', 'disabled' => array(2, '3x'),
'after' => '--after--',
'separator' => '--separator--',
'between' => array('--between first--', '--between second--')
)); ));
$expected = array( $expected = array(
'div' => array('class' => 'input radio'), 'div' => array('class' => 'input radio'),
'--before--',
'fieldset' => array(), 'fieldset' => array(),
'legend' => array(), 'legend' => array(),
'Field', 'Field',
@ -3747,50 +3818,26 @@ class FormHelperTest extends CakeTestCase {
array('label' => array('for' => 'ModelField1')), array('label' => array('for' => 'ModelField1')),
'first', 'first',
'/label', '/label',
'--between first--', array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
'--separator--',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
array('label' => array('for' => 'ModelField2')), array('label' => array('for' => 'ModelField2')),
'second', 'second',
'/label', '/label',
'--between second--', array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
array('label' => array('for' => 'ModelField2x')),
'2x',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
array('label' => array('for' => 'ModelField3')),
'third',
'/label',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
array('label' => array('for' => 'ModelField3x')),
'3x',
'/label',
'/fieldset', '/fieldset',
'--after--',
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'options' => array('1' => 'first', '2' => 'second'),
'type' => 'radio',
'before' => '--before--',
'after' => '--after--',
'separator' => '--separator--',
'between' => array('--between first--')
));
$expected = array(
'div' => array('class' => 'input radio'),
'--before--',
'fieldset' => array(),
'legend' => array(),
'Field',
'/legend',
array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
array('label' => array('for' => 'ModelField1')),
'first',
'/label',
'--between first--',
'--separator--',
array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
array('label' => array('for' => 'ModelField2')),
'second',
'/label',
'/fieldset',
'--after--',
'/div'
);
} }
/** /**
@ -4256,6 +4303,98 @@ class FormHelperTest extends CakeTestCase {
'/select' '/select'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
$selected = array('2', '3x');
$result = $this->Form->select(
'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
);
$expected = array(
'input' => array(
'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
),
'select' => array(
'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
),
array('option' => array('value' => '1')),
'One',
'/option',
array('option' => array('value' => '2', 'selected' => 'selected')),
'Two',
'/option',
array('option' => array('value' => '3')),
'Three',
'/option',
array('option' => array('value' => '3x', 'selected' => 'selected')),
'Stringy',
'/option',
'/select'
);
$this->assertTags($result, $expected);
}
/**
* Test generating multiple select with disabled elements.
*
* @return void
*/
public function testSelectMultipleWithDisabledElements() {
$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
$disabled = array(2, 3);
$result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
$expected = array(
'input' => array(
'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
),
'select' => array(
'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
),
array('option' => array('value' => '1')),
'One',
'/option',
array('option' => array('value' => '2', 'disabled' => 'disabled')),
'Two',
'/option',
array('option' => array('value' => '3', 'disabled' => 'disabled')),
'Three',
'/option',
array('option' => array('value' => '3x')),
'Stringy',
'/option',
'/select'
);
$this->assertTags($result, $expected);
$options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
$disabled = array('2', '3x');
$result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => 'ContactMultiple')),
'Multiple',
'/label',
'input' => array(
'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
),
'select' => array(
'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
),
array('option' => array('value' => '1')),
'One',
'/option',
array('option' => array('value' => '2', 'disabled' => 'disabled')),
'Two',
'/option',
array('option' => array('value' => '3')),
'Three',
'/option',
array('option' => array('value' => '3x', 'disabled' => 'disabled')),
'Stringy',
'/option',
'/select',
'/div'
);
$this->assertTags($result, $expected);
} }
/** /**

View file

@ -1510,7 +1510,8 @@ class FormHelper extends AppHelper {
if (isset($value) && strval($optValue) === strval($value)) { if (isset($value) && strval($optValue) === strval($value)) {
$optionsHere['checked'] = 'checked'; $optionsHere['checked'] = 'checked';
} }
if ($disabled && (!is_array($disabled) || in_array($optValue, $disabled))) { $isNumeric = is_numeric($optValue);
if ($disabled && (!is_array($disabled) || in_array((string)$optValue, $disabled, !$isNumeric))) {
$optionsHere['disabled'] = true; $optionsHere['disabled'] = true;
} }
$tagName = Inflector::camelize( $tagName = Inflector::camelize(
@ -2616,7 +2617,7 @@ class FormHelper extends AppHelper {
$isNumeric = is_numeric($name); $isNumeric = is_numeric($name);
if ( if (
(!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) || (!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
($selectedIsArray && in_array($name, $attributes['value'], !$isNumeric)) ($selectedIsArray && in_array((string)$name, $attributes['value'], !$isNumeric))
) { ) {
if ($attributes['style'] === 'checkbox') { if ($attributes['style'] === 'checkbox') {
$htmlOptions['checked'] = true; $htmlOptions['checked'] = true;
@ -2628,27 +2629,27 @@ class FormHelper extends AppHelper {
if ($showParents || (!in_array($title, $parents))) { if ($showParents || (!in_array($title, $parents))) {
$title = ($attributes['escape']) ? h($title) : $title; $title = ($attributes['escape']) ? h($title) : $title;
$hasDisabled = !empty($attributes['disabled']);
if ($hasDisabled) {
$disabledIsArray = is_array($attributes['disabled']);
if ($disabledIsArray) {
$disabledIsNumeric = is_numeric($name);
}
}
if (
$hasDisabled &&
$disabledIsArray &&
in_array((string)$name, $attributes['disabled'], !$disabledIsNumeric)
) {
$htmlOptions['disabled'] = 'disabled';
}
if ($hasDisabled && !$disabledIsArray) {
$htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
}
if ($attributes['style'] === 'checkbox') { if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name; $htmlOptions['value'] = $name;
$hasDisabled = !empty($attributes['disabled']);
if ($hasDisabled) {
$disabledIsArray = is_array($attributes['disabled']);
if ($disabledIsArray) {
$disabledIsNumeric = is_numeric($htmlOptions['value']);
}
}
if (
$hasDisabled &&
$disabledIsArray &&
in_array($htmlOptions['value'], $attributes['disabled'], !$disabledIsNumeric)
) {
$htmlOptions['disabled'] = 'disabled';
}
if ($hasDisabled && !$disabledIsArray) {
$htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
}
$tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name)); $tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name));
$htmlOptions['id'] = $tagName; $htmlOptions['id'] = $tagName;
$label = array('for' => $tagName); $label = array('for' => $tagName);