Completed
Pull Request — master (#647)
by Robbie
20:35 queued 18:30
created

UserFormsCheckboxSetField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 10 2
A validate() 0 22 3
1
<?php
2
3
namespace SilverStripe\UserForms\FormField;
4
5
use SilverStripe\Forms\CheckboxSetField;
6
7
/**
8
 * @package userforms
9
 */
10
class UserFormsCheckboxSetField extends CheckboxSetField
11
{
12
13
    /**
14
     * jQuery validate requires that the value of the option does not contain
15
     * the actual value of the input.
16
     *
17
     * @return ArrayList
18
     */
19
    public function getOptions()
20
    {
21
        $options = parent::getOptions();
22
23
        foreach ($options as $option) {
24
            $option->Name = "{$this->name}[]";
25
        }
26
27
        return $options;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     *
33
     * @param Validator $validator
34
     *
35
     * @return bool
36
     */
37
    public function validate($validator)
38
    {
39
        // get the previous values (could contain comma-delimited list)
40
41
        $previous = $value = $this->Value();
42
43
        if (is_string($value) && strstr($value, ",")) {
44
            $value = explode(",", $value);
45
        }
46
47
        // set the value as an array for parent validation
48
49
        $this->setValue($value);
50
51
        $validated = parent::validate($validator);
52
53
        // restore previous value after validation
54
55
        $this->setValue($previous);
56
57
        return $validated;
58
    }
59
}
60