|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
6
|
|
|
use SilverStripe\Core\Extensible; |
|
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
8
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This validation class handles all form and custom form validation through the use of Required |
|
12
|
|
|
* fields. It relies on javascript for client-side validation, and marking fields after server-side |
|
13
|
|
|
* validation. It acts as a visitor to individual form fields. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Validator |
|
16
|
|
|
{ |
|
17
|
|
|
use Injectable; |
|
18
|
|
|
use Configurable; |
|
19
|
|
|
use Extensible; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->resetResult(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Form $form |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $form; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ValidationResult $result |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $result; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
private $enabled = true; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Form $form |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setForm($form) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->form = $form; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns any errors there may be. |
|
53
|
|
|
* |
|
54
|
|
|
* @return ValidationResult |
|
55
|
|
|
*/ |
|
56
|
|
|
public function validate() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->resetResult(); |
|
59
|
|
|
if ($this->getEnabled()) { |
|
60
|
|
|
$this->php($this->form->getData()); |
|
61
|
|
|
} |
|
62
|
|
|
return $this->result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Callback to register an error on a field (Called from implementations of |
|
67
|
|
|
* {@link FormField::validate}). The optional error message type parameter is loaded into the |
|
68
|
|
|
* HTML class attribute. |
|
69
|
|
|
* |
|
70
|
|
|
* See {@link getErrors()} for details. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $fieldName Field name for this error |
|
73
|
|
|
* @param string $message The message string |
|
74
|
|
|
* @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS |
|
75
|
|
|
* class to the form, so other values can be used if desired. |
|
76
|
|
|
* @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
|
77
|
|
|
* Bool values will be treated as plain text flag. |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function validationError( |
|
81
|
|
|
$fieldName, |
|
82
|
|
|
$message, |
|
83
|
|
|
$messageType = ValidationResult::TYPE_ERROR, |
|
84
|
|
|
$cast = ValidationResult::CAST_TEXT |
|
85
|
|
|
) { |
|
86
|
|
|
$this->result->addFieldError($fieldName, $message, $messageType, null, $cast); |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns all errors found by a previous call to {@link validate()}. The returned array has a |
|
92
|
|
|
* structure resembling: |
|
93
|
|
|
* |
|
94
|
|
|
* <code> |
|
95
|
|
|
* array( |
|
96
|
|
|
* 'fieldName' => '[form field name]', |
|
97
|
|
|
* 'message' => '[validation error message]', |
|
98
|
|
|
* 'messageType' => '[bad|message|validation|required]', |
|
99
|
|
|
* 'messageCast' => '[text|html]' |
|
100
|
|
|
* ) |
|
101
|
|
|
* </code> |
|
102
|
|
|
* |
|
103
|
|
|
* @return null|array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getErrors() |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->result) { |
|
108
|
|
|
return $this->result->getMessages(); |
|
109
|
|
|
} |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get last validation result |
|
115
|
|
|
* |
|
116
|
|
|
* @return ValidationResult |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getResult() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->result; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns whether the field in question is required. This will usually display '*' next to the |
|
125
|
|
|
* field. The base implementation always returns false. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $fieldName |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function fieldIsRequired($fieldName) |
|
132
|
|
|
{ |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param array $data |
|
138
|
|
|
* |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
|
|
abstract public function php($data); |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param bool $enabled |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setEnabled($enabled) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->enabled = (bool)$enabled; |
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getEnabled() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->enabled; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return $this |
|
163
|
|
|
*/ |
|
164
|
|
|
public function removeValidation() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->setEnabled(false); |
|
167
|
|
|
$this->resetResult(); |
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* When Validators are set on the form, it can affect whether or not the form cannot be cached. |
|
173
|
|
|
* |
|
174
|
|
|
* @see RequiredFields for an example of when you might be able to cache your form. |
|
175
|
|
|
* |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
|
|
public function canBeCached(): bool |
|
179
|
|
|
{ |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Clear current result |
|
185
|
|
|
* |
|
186
|
|
|
* @return $this |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function resetResult() |
|
189
|
|
|
{ |
|
190
|
|
|
$this->result = ValidationResult::create(); |
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|