silverstripe-userforms/code/formfields/UserFormsCheckboxSetField.php

68 lines
1.3 KiB
PHP
Raw Normal View History

<?php
/**
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class UserFormsCheckboxSetField extends CheckboxSetField
{
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
*
* @return array
*/
public function getSourceAsArray()
{
$array = parent::getSourceAsArray();
2016-07-21 07:53:59 +02:00
return array_values($array);
}
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;
}
}