mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
Fixing validation for CheckboxSet field (multiple options selected in a required field)
This commit is contained in:
parent
23cd591ae6
commit
a92c9ec69d
@ -20,4 +20,46 @@ class UserFormsCheckboxSetField extends CheckboxSetField {
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user