Skip to content
Browse files

Update FormHelper::radio().

It now passes all the currently unskipped tests related to radio
buttons. Most of the logic has been moved to Widget\Radio and doesn't
need to be in the helper anymore.
  • Loading branch information...
1 parent 0a0bff0 commit 4b9b98902c952a071d13718762762ac26fce02f4 @markstory markstory committed
Showing with 16 additions and 251 deletions.
  1. +16 −105 src/View/Helper/FormHelper.php
  2. +0 −146 tests/TestCase/View/Helper/FormHelperTest.php
View
121 src/View/Helper/FormHelper.php
@@ -173,6 +173,9 @@ class FormHelper extends Helper {
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
+ 'label' => '<label{{attrs}}>{{text}}</label>',
+ 'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
+ 'radioContainer' => '{{input}}{{label}}',
'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
];
@@ -1389,16 +1392,11 @@ public function checkbox($fieldName, $options = array()) {
}
/**
- * Creates a set of radio widgets. Will create a legend and fieldset
- * by default. Use $options to control this
+ * Creates a set of radio widgets.
*
* ### Attributes:
*
- * - `separator` - define the string in between the radio buttons
- * - `between` - the string between legend and input set or array of strings to insert
- * strings between each input block
- * - `legend` - control whether or not the widget set has a fieldset & legend
- * - `value` - indicate a value that is should be checked
+ * - `value` - Indicate a value that is should be checked
* - `label` - boolean to indicate whether or not labels for widgets show be displayed
* - `hiddenField` - boolean to indicate if you want the results of radio() to include
* a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
@@ -1412,109 +1410,24 @@ public function checkbox($fieldName, $options = array()) {
* @return string Completed radio widget set.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
- public function radio($fieldName, $options = array(), $attributes = array()) {
+ public function radio($fieldName, $options = [], $attributes = []) {
$attributes = $this->_initInputField($fieldName, $attributes);
- $showEmpty = $this->_extractOption('empty', $attributes);
- if ($showEmpty) {
- $showEmpty = ($showEmpty === true) ? __d('cake', 'empty') : $showEmpty;
- $options = array('' => $showEmpty) + $options;
- }
- unset($attributes['empty']);
-
- $legend = false;
- if (isset($attributes['legend'])) {
- $legend = $attributes['legend'];
- unset($attributes['legend']);
- } elseif (count($options) > 1) {
- $legend = __(Inflector::humanize($this->field()));
- }
-
- $label = true;
- if (isset($attributes['label'])) {
- $label = $attributes['label'];
- unset($attributes['label']);
- }
-
- $separator = null;
- if (isset($attributes['separator'])) {
- $separator = $attributes['separator'];
- unset($attributes['separator']);
- }
-
- $between = null;
- if (isset($attributes['between'])) {
- $between = $attributes['between'];
- unset($attributes['between']);
- }
-
- $value = null;
- if (isset($attributes['value'])) {
- $value = $attributes['value'];
- } else {
- $value = $this->value($fieldName);
- }
-
- $disabled = array();
- if (isset($attributes['disabled'])) {
- $disabled = $attributes['disabled'];
- }
-
- $out = array();
-
+ $out = [];
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
unset($attributes['hiddenField']);
- if (isset($value) && is_bool($value)) {
- $value = $value ? 1 : 0;
+ $value = $attributes['val'];
+ $hidden = '';
+ if ($hiddenField && (!isset($value) || $value === '')) {
+ $hidden = $this->hidden($fieldName, [
+ 'value' => '',
+ 'name' => $attributes['name']
+ ]);
}
+ $attributes['options'] = $options;
- $this->_domIdSuffixes = array();
- foreach ($options as $optValue => $optTitle) {
- $optionsHere = array('value' => $optValue, 'disabled' => false);
-
- if (isset($value) && strval($optValue) === strval($value)) {
- $optionsHere['checked'] = 'checked';
- }
- $isNumeric = is_numeric($optValue);
- if ($disabled && (!is_array($disabled) || in_array((string)$optValue, $disabled, !$isNumeric))) {
- $optionsHere['disabled'] = true;
- }
- $tagName = $attributes['id'] . $this->domIdSuffix($optValue);
-
- if ($label) {
- $labelOpts = is_array($label) ? $label : array();
- $labelOpts += array('for' => $tagName);
- $optTitle = $this->label($tagName, $optTitle, $labelOpts);
- }
-
- if (is_array($between)) {
- $optTitle .= array_shift($between);
- }
- $allOptions = array_merge($attributes, $optionsHere);
- $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
- array_diff_key($allOptions, array('name' => null, 'type' => null, 'id' => null)),
- $optTitle
- );
- }
- $hidden = null;
-
- if ($hiddenField) {
- if (!isset($value) || $value === '') {
- $hidden = $this->hidden($fieldName, array(
- 'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name']
- ));
- }
- }
- $out = $hidden . implode($separator, $out);
-
- if (is_array($between)) {
- $between = '';
- }
- if ($legend) {
- $out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $between . $out);
- }
- return $out;
+ return $hidden . $this->widget('radio', $attributes);
}
/**
@@ -2878,8 +2791,6 @@ protected function _initInputField($field, $options = []) {
}
unset($options['value'], $options['default']);
- $options += (array)$context->attributes($field);
-
if ($context->hasError($field)) {
$options = $this->addClass($options, $this->settings['errorClass']);
}
View
146 tests/TestCase/View/Helper/FormHelperTest.php
@@ -4288,152 +4288,6 @@ public function testRadio() {
}
/**
- * test disabled radio options
- *
- * @return void
- */
- public function testRadioDisabled() {
- $this->markTestIncomplete('Need to revisit once models work again.');
- $result = $this->Form->radio(
- 'Model.field',
- array('option A', 'option B'),
- array('disabled' => array(0), 'value' => '0')
- );
- $expected = array(
- 'fieldset' => array(),
- 'legend' => array(),
- 'Field',
- '/legend',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
- array('label' => array('for' => 'ModelField0')),
- 'option A',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
- array('label' => array('for' => 'ModelField1')),
- 'option B',
- '/label',
- '/fieldset'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->radio(
- 'Model.field',
- array('option A', 'option B'),
- array('disabled' => true, 'value' => '0')
- );
- $expected = array(
- 'fieldset' => array(),
- 'legend' => array(),
- 'Field',
- '/legend',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
- array('label' => array('for' => 'ModelField0')),
- 'option A',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
- array('label' => array('for' => 'ModelField1')),
- 'option B',
- '/label',
- '/fieldset'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->radio(
- 'Model.field',
- array('option A', 'option B'),
- array('disabled' => 'disabled', 'value' => '0')
- );
- $expected = array(
- 'fieldset' => array(),
- 'legend' => array(),
- 'Field',
- '/legend',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
- array('label' => array('for' => 'ModelField0')),
- 'option A',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
- array('label' => array('for' => 'ModelField1')),
- 'option B',
- '/label',
- '/fieldset'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('Model.field', array(
- 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
- 'type' => 'radio',
- 'disabled' => array(2, '3x'),
- ));
-
- $expected = array(
- 'div' => array('class' => 'input radio'),
- 'fieldset' => array(),
- 'legend' => array(),
- 'Field',
- '/legend',
- array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
- array('label' => array('for' => 'ModelField1')),
- 'first',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
- array('label' => array('for' => 'ModelField2')),
- 'second',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')),
- array('label' => array('for' => 'ModelField2x')),
- '2x',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')),
- array('label' => array('for' => 'ModelField3')),
- 'third',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
- array('label' => array('for' => 'ModelField3x')),
- '3x',
- '/label',
- '/fieldset',
- '/div'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('Model.field', array(
- 'type' => 'radio',
- 'options' => array(
- 1 => 'A',
- 2 => 'B',
- 3 => 'C'
- ),
- 'disabled' => array(1)
- ));
-
- $expected = array(
- 'div' => array('class' => 'input radio'),
- '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]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
- array('label' => array('for' => 'ModelField1')),
- 'A',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
- array('label' => array('for' => 'ModelField2')),
- 'B',
- '/label',
- array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
- array('label' => array('for' => 'ModelField3')),
- 'C',
- '/label',
- '/fieldset',
- '/div'
- );
- $this->assertTags($result, $expected);
- }
-
-/**
* test disabling the hidden input for radio buttons
*
* @return void

0 comments on commit 4b9b989

Please sign in to comment.
Something went wrong with that request. Please try again.