silverstripe-userforms/code/FormField/UserFormsCheckboxSetField.php

80 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\FormField;
use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\UserForms\Model\EditableFormField;
/**
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class UserFormsCheckboxSetField extends CheckboxSetField
{
2016-07-21 07:53:59 +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();
2016-07-21 07:53:59 +02:00
foreach ($options as $option) {
$option->Name = "{$this->name}[]";
}
2016-07-21 07:53:59 +02:00
return $options;
}
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-07-21 07:53:59 +02:00
$previous = $value = $this->Value();
2016-07-21 07:53:59 +02:00
if (is_string($value) && strstr($value, ",")) {
$value = explode(",", $value);
}
2016-07-21 07:53:59 +02:00
// set the value as an array for parent validation
2016-07-21 07:53:59 +02:00
$this->setValue($value);
2016-07-21 07:53:59 +02:00
$validated = parent::validate($validator);
2016-07-21 07:53:59 +02:00
// restore previous value after validation
2016-07-21 07:53:59 +02:00
$this->setValue($previous);
2016-07-21 07:53:59 +02:00
return $validated;
}
}