Completed
Push — master ( 069f29...ab05a4 )
by Damian
31:40
created

UserFormsCheckboxSetField::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/**
4
 * @package userforms
5
 */
6
class UserFormsCheckboxSetField extends CheckboxSetField {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
8
	/**
9
	 * jQuery validate requires that the value of the option does not contain
10
	 * the actual value of the input.
11
	 *
12
	 * @return ArrayList
13
	 */
14
	public function getOptions() {
15
		$options = parent::getOptions();
16
17
		foreach($options as $option) {
18
			$option->Name = "{$this->name}[]";
19
		}
20
21
		return $options;
22
	}
23
24
	/**
25
	 * @inheritdoc
26
	 *
27
	 * @return array
28
	 */
29
	public function getSourceAsArray()
30
	{
31
		$array = parent::getSourceAsArray();
32
33
		return array_values($array);
34
	}
35
36
	/**
37
	 * @inheritdoc
38
	 *
39
	 * @param Validator $validator
40
	 *
41
	 * @return bool
42
	 */
43
	public function validate($validator)
44
	{
45
		// get the previous values (could contain comma-delimited list)
46
47
		$previous = $value = $this->Value();
48
49
		if (strstr($value, ",")) {
50
			$value = explode(",", $value);
51
		}
52
53
		// set the value as an array for parent validation
54
55
		$this->setValue($value);
56
57
		$validated = parent::validate($validator);
58
59
		// restore previous value after validation
60
61
		$this->setValue($previous);
62
63
		return $validated;
64
	}
65
}
66