1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This validation class handles all form and custom form validation through the use of Required |
5
|
|
|
* fields. It relies on javascript for client-side validation, and marking fields after server-side |
6
|
|
|
* validation. It acts as a visitor to individual form fields. |
7
|
|
|
* |
8
|
|
|
* @package forms |
9
|
|
|
* @subpackage validators |
10
|
|
|
*/ |
11
|
|
|
abstract class Validator extends SS_Object { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Form $form |
15
|
|
|
*/ |
16
|
|
|
protected $form; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array $errors |
20
|
|
|
*/ |
21
|
|
|
protected $errors; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Form $form |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
public function setForm($form) { |
29
|
|
|
$this->form = $form; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns any errors there may be. |
36
|
|
|
* |
37
|
|
|
* @return null|array |
38
|
|
|
*/ |
39
|
|
|
public function validate() { |
40
|
|
|
$this->errors = null; |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->php($this->form->getData()); |
43
|
|
|
|
44
|
|
|
return $this->errors; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Callback to register an error on a field (Called from implementations of |
49
|
|
|
* {@link FormField::validate}). The optional error message type parameter is loaded into the |
50
|
|
|
* HTML class attribute. |
51
|
|
|
* |
52
|
|
|
* See {@link getErrors()} for details. |
53
|
|
|
* |
54
|
|
|
* @param string $fieldName |
55
|
|
|
* @param string $errorMessage |
56
|
|
|
* @param string $errorMessageType |
57
|
|
|
*/ |
58
|
|
|
public function validationError($fieldName, $errorMessage, $errorMessageType = '') { |
59
|
|
|
$this->errors[] = array( |
60
|
|
|
'fieldName' => $fieldName, |
61
|
|
|
'message' => $errorMessage, |
62
|
|
|
'messageType' => $errorMessageType, |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns all errors found by a previous call to {@link validate()}. The returned array has a |
68
|
|
|
* structure resembling: |
69
|
|
|
* |
70
|
|
|
* <code> |
71
|
|
|
* array( |
72
|
|
|
* 'fieldName' => '[form field name]', |
73
|
|
|
* 'message' => '[validation error message]', |
74
|
|
|
* 'messageType' => '[bad|message|validation|required]', |
75
|
|
|
* ) |
76
|
|
|
* </code> |
77
|
|
|
* |
78
|
|
|
* @return null|array |
79
|
|
|
*/ |
80
|
|
|
public function getErrors() { |
81
|
|
|
return $this->errors; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $fieldName |
86
|
|
|
* @param array $data |
87
|
|
|
*/ |
88
|
|
|
public function requireField($fieldName, $data) { |
89
|
|
|
if(is_array($data[$fieldName]) && count($data[$fieldName])) { |
90
|
|
|
foreach($data[$fieldName] as $componentKey => $componentValue) { |
91
|
|
|
if(!strlen($componentValue)) { |
92
|
|
|
$this->validationError( |
93
|
|
|
$fieldName, |
94
|
|
|
sprintf('%s %s is required', $fieldName, $componentKey), |
95
|
|
|
'required' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} else if(!strlen($data[$fieldName])) { |
100
|
|
|
$this->validationError( |
101
|
|
|
$fieldName, |
102
|
|
|
sprintf('%s is required', $fieldName), |
103
|
|
|
'required' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns whether the field in question is required. This will usually display '*' next to the |
110
|
|
|
* field. The base implementation always returns false. |
111
|
|
|
* |
112
|
|
|
* @param string $fieldName |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function fieldIsRequired($fieldName) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $data |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
abstract public function php($data); |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..