|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Validation\Field; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Error\Error; |
|
17
|
|
|
use Romm\Formz\Error\Notice; |
|
18
|
|
|
use Romm\Formz\Error\Warning; |
|
19
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
|
20
|
|
|
use Romm\Formz\Form\FormInterface; |
|
21
|
|
|
use Romm\Formz\Service\MessageService; |
|
22
|
|
|
use Romm\Formz\Service\ValidatorService; |
|
23
|
|
|
use Romm\Formz\Validation\Field\DataObject\ValidatorDataObject; |
|
24
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; |
|
25
|
|
|
|
|
26
|
|
|
abstract class AbstractFieldValidator extends AbstractValidator |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Fill with paths to JavaScript files containing validation code. They will |
|
31
|
|
|
* be automatically imported when needed. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static $javaScriptValidationFiles = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* List of supported messages, which are used whenever an error occurs. |
|
39
|
|
|
* Can be overridden with TypoScript in the validator configuration. |
|
40
|
|
|
* |
|
41
|
|
|
* Example: |
|
42
|
|
|
* $supportedMessages = [ |
|
43
|
|
|
* 'default' => [ |
|
44
|
|
|
* 'key' => 'path.to.my.message', |
|
45
|
|
|
* 'extension' => 'extension_containing_message', |
|
46
|
|
|
* 'value' => 'Some value' // Static value of the message, not recommended though. |
|
47
|
|
|
* ] |
|
48
|
|
|
* ] |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $supportedMessages = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set this to true if you want to be able to add any message you want. |
|
56
|
|
|
* |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $supportsAllMessages = false; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Contains the original form instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @var FormInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $form; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Contains the merge of the supported messages and the TypoScript defined |
|
70
|
|
|
* messages. |
|
71
|
|
|
* |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $messages = []; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Array of arbitral data which can be added by child classes, and will |
|
78
|
|
|
* then be added to the `$validationData` property of the form instance. |
|
79
|
|
|
* |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $validationData = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var ValidatorDataObject |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $dataObject; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Constructs the validator, sets validation options and messages. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $options Options for the validator. |
|
93
|
|
|
* @param ValidatorDataObject $dataObject |
|
94
|
|
|
*/ |
|
95
|
|
|
final public function __construct(array $options = [], ValidatorDataObject $dataObject) |
|
96
|
|
|
{ |
|
97
|
|
|
parent::__construct($options); |
|
98
|
|
|
|
|
99
|
|
|
$this->dataObject = $dataObject; |
|
100
|
|
|
$this->form = $dataObject->getFormObject()->getForm(); |
|
101
|
|
|
$this->messages = ValidatorService::get()->filterMessages( |
|
102
|
|
|
$this->dataObject->getValidator(), |
|
103
|
|
|
$this->supportedMessages, |
|
104
|
|
|
(bool)$this->supportsAllMessages |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Creates a new validation error and adds it to the result. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @param int $code |
|
113
|
|
|
* @param array $arguments |
|
114
|
|
|
* @param string $title |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function addError($key, $code, array $arguments = [], $title = '') |
|
117
|
|
|
{ |
|
118
|
|
|
$message = $this->addMessage(Error::class, $key, $code, $arguments, $title); |
|
119
|
|
|
$this->result->addError($message); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Creates a new validation warning and adds it to the result. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $key |
|
126
|
|
|
* @param int $code |
|
127
|
|
|
* @param array $arguments |
|
128
|
|
|
* @param string $title |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function addWarning($key, $code, array $arguments = [], $title = '') |
|
131
|
|
|
{ |
|
132
|
|
|
$message = $this->addMessage(Warning::class, $key, $code, $arguments, $title); |
|
133
|
|
|
$this->result->addWarning($message); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Creates a new validation notice and adds it to the result. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $key |
|
140
|
|
|
* @param int $code |
|
141
|
|
|
* @param array $arguments |
|
142
|
|
|
* @param string $title |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function addNotice($key, $code, array $arguments = [], $title = '') |
|
145
|
|
|
{ |
|
146
|
|
|
$message = $this->addMessage(Notice::class, $key, $code, $arguments, $title); |
|
147
|
|
|
$this->result->addNotice($message); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the full validation data. |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getValidationData() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->validationData; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Refreshes entirely the validation data (see `setValidationDataValue()`). |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $validationData |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function setValidationData(array $validationData) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->validationData = array_merge($this->validationData, $validationData); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Adds an arbitral value to the validator, which will be added to the |
|
172
|
|
|
* `$validationData` property of the form. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $key Key of the data. |
|
175
|
|
|
* @param mixed $value Value bound to the key. |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function setValidationDataValue($key, $value) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->validationData[$key] = $value; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $type |
|
184
|
|
|
* @param string $key |
|
185
|
|
|
* @param string $code |
|
186
|
|
|
* @param array $arguments |
|
187
|
|
|
* @param string $title |
|
188
|
|
|
* @return mixed |
|
189
|
|
|
* @throws EntryNotFoundException |
|
190
|
|
|
*/ |
|
191
|
|
|
private function addMessage($type, $key, $code, array $arguments, $title) |
|
192
|
|
|
{ |
|
193
|
|
|
if (!isset($this->messages[$key])) { |
|
194
|
|
|
throw EntryNotFoundException::errorKeyNotFoundForValidator($key, $this); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return new $type( |
|
198
|
|
|
$this->getMessage($key, $arguments), |
|
199
|
|
|
$code, |
|
200
|
|
|
$this->dataObject->getValidator()->getName(), |
|
201
|
|
|
$key, |
|
202
|
|
|
[], |
|
203
|
|
|
$title |
|
204
|
|
|
); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* This function should *always* be used when a message should be translated |
|
209
|
|
|
* when an error occurs in the validation process. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $key The key of the message, usually "default". |
|
212
|
|
|
* @param array $arguments Arguments given to the message. |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
private function getMessage($key, array $arguments = []) |
|
216
|
|
|
{ |
|
217
|
|
|
return MessageService::get()->parseMessageArray($this->messages[$key], $arguments); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
|
|
public static function getJavaScriptValidationFiles() |
|
224
|
|
|
{ |
|
225
|
|
|
return static::$javaScriptValidationFiles; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|