From c1343395263fe341da8670f4e0f5678510e9e75d Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Thu, 9 Feb 2023 15:37:54 +0100 Subject: [PATCH] Fixed bug with option fields. 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. --- src/FuzeWorks/Forms/Fields/RadioField.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/FuzeWorks/Forms/Fields/RadioField.php b/src/FuzeWorks/Forms/Fields/RadioField.php index 4d3f58d..33b5946 100644 --- a/src/FuzeWorks/Forms/Fields/RadioField.php +++ b/src/FuzeWorks/Forms/Fields/RadioField.php @@ -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 .= ""; 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); + } } \ No newline at end of file