Navigation Menu

Skip to content

Commit

Permalink
Fix disabled attribute calculation.
Browse files Browse the repository at this point in the history
Take the work done in #10770 and account for the complex options and
non-empty disabled option sets.

Refs #10770
  • Loading branch information
markstory committed Jun 16, 2017
1 parent e123f00 commit e8090bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/View/Helper/FormHelper.php
Expand Up @@ -2577,17 +2577,7 @@ protected function _initInputField($field, $options = [])
if ($context->hasError($field)) {
$options = $this->addClass($options, $this->_config['errorClass']);
}
$isDisabled = false;
if (isset($options['disabled'])) {
$isDisabled = (
$options['disabled'] === true ||
$options['disabled'] === 'disabled' ||
(is_array($options['disabled']) &&
!empty($options['options']) &&
array_diff($options['options'], $options['disabled']) === []
)
);
}
$isDisabled = $this->_isDisabled($options);
if ($isDisabled) {
$options['secure'] = self::SECURE_SKIP;
}
Expand All @@ -2601,6 +2591,42 @@ protected function _initInputField($field, $options = [])
return $options;
}

/**
* Determine if a field is disabled.
*
* @param array $options The option set.
* @return bool Whether or not the field is disabled.
*/
protected function _isDisabled(array $options)
{
if (!isset($options['disabled'])) {
return false;
}
if (is_scalar($options['disabled'])) {
return ($options['disabled'] === true || $options['disabled'] === 'disabled');
}
if (!isset($options['options'])) {
return false;
}
if (is_array($options['options'])) {
// Simple list options
$first = $options['options'][array_keys($options['options'])[0]];
if (is_scalar($first)) {
return array_diff($options['options'], $options['disabled']) === [];
}
// Complex option types
if (is_array($first)) {
$disabled = array_filter($options['options'], function ($i) use ($options) {
return in_array($i['value'], $options['disabled']);
});

return count($disabled) > 0;
}
}

return false;
}

/**
* Get the field name for use with _secure().
*
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -4627,6 +4627,37 @@ public function testRadio()
$this->assertHtml($expected, $result);
}

/**
* Test radio with complex options and empty disabled data.
*
* @return void
*/
public function testRadioComplexDisabled()
{
$options = [
['value' => 'r', 'text' => 'red'],
['value' => 'b', 'text' => 'blue'],
];
$attrs = ['disabled' => []];
$result = $this->Form->radio('Model.field', $options, $attrs);
$expected = [
'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
['label' => ['for' => 'model-field-r']],
['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
'red',
'/label',
['label' => ['for' => 'model-field-b']],
['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
'blue',
'/label',
];
$this->assertHtml($expected, $result);

$attrs = ['disabled' => ['r']];
$result = $this->Form->radio('Model.field', $options, $attrs);
$this->assertContains('disabled="disabled"', $result);
}

/**
* testRadioDefaultValue method
*
Expand Down

0 comments on commit e8090bc

Please sign in to comment.