Fixed bug with option fields.
continuous-integration/drone/push Build is passing Details

If the addOptions() method was used on RadioField or SelectField, and the array was provided as numeric, it would wrongly get added as numeric and values would not be associated correctly with their options. This was especially troublesome for the first element in the options array, as that element would be impossible to select.
This commit is contained in:
Abel Hoogeveen 2023-02-09 15:37:54 +01:00
parent 724f7b96e6
commit c134339526
1 changed files with 15 additions and 0 deletions

View File

@ -75,6 +75,9 @@ class RadioField extends Field
*/
public function addOptions(array $options): self
{
if (!$this->_isAssoc($options))
throw new InputException("Could not add options. Options must be provided as an associative array [name => label]");
foreach ($options as $key => $val)
$this->addOption($key, $val);
@ -134,4 +137,16 @@ class RadioField extends Field
$out .= "</div>";
return $out;
}
/**
* Internal method for addOptions()
*
* @param array $arr
* @return bool
*/
private function _isAssoc(array $arr): bool
{
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
}