From a92c9ec69d4901258ecb10cd6be0023c69c16247 Mon Sep 17 00:00:00 2001 From: assertchris Date: Mon, 16 May 2016 08:25:09 +1200 Subject: [PATCH] Fixing validation for CheckboxSet field (multiple options selected in a required field) --- code/formfields/UserFormsCheckboxSetField.php | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/code/formfields/UserFormsCheckboxSetField.php b/code/formfields/UserFormsCheckboxSetField.php index 9a7621e..f8f7d14 100644 --- a/code/formfields/UserFormsCheckboxSetField.php +++ b/code/formfields/UserFormsCheckboxSetField.php @@ -20,4 +20,46 @@ class UserFormsCheckboxSetField extends CheckboxSetField { return $options; } -} \ No newline at end of file + + /** + * @inheritdoc + * + * @return array + */ + public function getSourceAsArray() + { + $array = parent::getSourceAsArray(); + + return array_values($array); + } + + /** + * @inheritdoc + * + * @param Validator $validator + * + * @return bool + */ + public function validate($validator) + { + // get the previous values (could contain comma-delimited list) + + $previous = $value = $this->Value(); + + if (strstr($value, ",")) { + $value = explode(",", $value); + } + + // set the value as an array for parent validation + + $this->setValue($value); + + $validated = parent::validate($validator); + + // restore previous value after validation + + $this->setValue($previous); + + return $validated; + } +}