2014-07-25 04:52:56 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\FormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxSetField;
|
2020-07-21 07:30:55 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2014-07-25 04:52:56 +02:00
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserFormsCheckboxSetField extends CheckboxSetField
|
|
|
|
{
|
2014-07-25 04:52:56 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
2020-07-21 07:30:55 +02:00
|
|
|
* If your project uses a custom UserFormsCheckboxSetField.ss, ensure that it includes
|
|
|
|
* `$Top.getValidationAttributesHTML().RAW` so that custom validation messages work
|
|
|
|
* For further details see
|
|
|
|
* templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField.ss
|
|
|
|
*
|
|
|
|
* Use on a template with .RAW - single and double quoted strings will be safely escaped
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @see EditableFormField::updateFormField()
|
|
|
|
*/
|
|
|
|
public function getValidationAttributesHTML()
|
|
|
|
{
|
|
|
|
$attrs = array_filter(array_keys($this->getAttributes()), function ($attr) {
|
|
|
|
return !in_array($attr, ['data-rule-required', 'data-msg-required']);
|
|
|
|
});
|
|
|
|
return $this->getAttributesHTML(...$attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-21 07:53:59 +02:00
|
|
|
* jQuery validate requires that the value of the option does not contain
|
|
|
|
* the actual value of the input.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getOptions()
|
|
|
|
{
|
|
|
|
$options = parent::getOptions();
|
2014-07-25 04:52:56 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($options as $option) {
|
|
|
|
$option->Name = "{$this->name}[]";
|
|
|
|
}
|
2014-07-25 04:52:56 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $options;
|
|
|
|
}
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate($validator)
|
|
|
|
{
|
|
|
|
// get the previous values (could contain comma-delimited list)
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$previous = $value = $this->Value();
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if (is_string($value) && strstr($value, ",")) {
|
|
|
|
$value = explode(",", $value);
|
|
|
|
}
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// set the value as an array for parent validation
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->setValue($value);
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$validated = parent::validate($validator);
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// restore previous value after validation
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->setValue($previous);
|
2016-05-15 22:25:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $validated;
|
|
|
|
}
|
2016-05-15 22:25:09 +02:00
|
|
|
}
|