1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 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\Validator; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Field\Validation\Message as FormzMessage; |
17
|
|
|
use Romm\Formz\Core\Core; |
18
|
|
|
use Romm\Formz\Error\Error; |
19
|
|
|
use Romm\Formz\Error\Notice; |
20
|
|
|
use Romm\Formz\Error\Warning; |
21
|
|
|
use Romm\Formz\Form\FormInterface; |
22
|
|
|
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator; |
23
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; |
26
|
|
|
|
27
|
|
|
abstract class AbstractValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Fill with paths to JavaScript files containing validation code. They will |
32
|
|
|
* be automatically imported when needed. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected static $javaScriptValidationFiles = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* List of supported messages, which are used whenever an error occurs. |
40
|
|
|
* Can be overridden with TypoScript in the validator configuration. |
41
|
|
|
* |
42
|
|
|
* Example: |
43
|
|
|
* $supportedMessages = [ |
44
|
|
|
* 'default' => [ |
45
|
|
|
* 'key' => 'path.to.my.message', |
46
|
|
|
* 'extension' => 'extension_containing_message', |
47
|
|
|
* 'value' => 'Some value' // Static value of the message, not recommended though. |
48
|
|
|
* ] |
49
|
|
|
* ] |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $supportedMessages = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set this to true if you want to be able to add any message you want. |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $supportsAllMessages = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Contains the original form instance. |
64
|
|
|
* |
65
|
|
|
* @var FormInterface |
66
|
|
|
*/ |
67
|
|
|
protected $form; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Contains the name of the field which is being validated by this |
71
|
|
|
* validator. |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $fieldName; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Contains the merge of the supported messages and the TypoScript defined |
79
|
|
|
* messages. |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
protected $messages = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Array of arbitral data which can be added by child classes, and will |
87
|
|
|
* then be added to the `$validationData` property of the form instance. |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
protected $validationData = []; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var Dispatcher |
95
|
|
|
*/ |
96
|
|
|
protected $signalSlotDispatcher; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Constructs the validator, sets validation options and messages. |
100
|
|
|
* |
101
|
|
|
* @param array $options Options for the validator. |
102
|
|
|
* @param FormInterface $form The original form. |
103
|
|
|
* @param string $field Name of the field being validated. |
104
|
|
|
* @param FormzMessage[] $messages Messages for the validator. |
105
|
|
|
* @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException |
106
|
|
|
*/ |
107
|
|
|
public function __construct(array $options = [], $form = null, $field = '', array $messages = []) |
108
|
|
|
{ |
109
|
|
|
parent::__construct($options); |
110
|
|
|
|
111
|
|
|
$this->signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class); |
112
|
|
|
$this->form = $form; |
113
|
|
|
$this->fieldName = $field; |
114
|
|
|
|
115
|
|
|
$this->messages = $this->injectMessages($messages); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Will manage the messages of the current validator: only the supported |
120
|
|
|
* messages will be handled. |
121
|
|
|
* |
122
|
|
|
* @param FormzMessage[] $messages |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function injectMessages(array $messages) |
126
|
|
|
{ |
127
|
|
|
// Adding the keys `value` and `extension` to the messages, only if it is missing. |
128
|
|
|
$addValueToArray = function (array &$a) { |
129
|
|
|
foreach ($a as $k => $v) { |
130
|
|
|
if (false === isset($v['value'])) { |
131
|
|
|
$a[$k]['value'] = ''; |
132
|
|
|
} |
133
|
|
|
if (false === isset($v['extension'])) { |
134
|
|
|
$a[$k]['extension'] = ''; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $a; |
139
|
|
|
}; |
140
|
|
|
|
141
|
|
|
$messagesArray = []; |
142
|
|
|
foreach ($messages as $key => $message) { |
143
|
|
|
if (false === is_array($message)) { |
144
|
|
|
$message = $message->toArray(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$messagesArray[$key] = $message; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$addValueToArray($messagesArray); |
151
|
|
|
$addValueToArray($this->supportedMessages); |
152
|
|
|
|
153
|
|
|
$messagesResult = $this->supportedMessages; |
154
|
|
|
|
155
|
|
|
ArrayUtility::mergeRecursiveWithOverrule( |
156
|
|
|
$messagesResult, |
157
|
|
|
$messagesArray, |
158
|
|
|
(true === $this->supportsAllMessages) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
return $messagesResult; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Creates a new validation error and adds it to the result. |
166
|
|
|
* |
167
|
|
|
* @param string $key |
168
|
|
|
* @param int $code |
169
|
|
|
* @param array $arguments |
170
|
|
|
* @param string $title |
171
|
|
|
*/ |
172
|
|
|
protected function addError($key, $code, array $arguments = [], $title = '') |
173
|
|
|
{ |
174
|
|
|
$message = $this->addMessage(Error::class, $key, $code, $arguments, $title); |
175
|
|
|
$this->result->addError($message); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Creates a new validation warning and adds it to the result. |
180
|
|
|
* |
181
|
|
|
* @param string $key |
182
|
|
|
* @param int $code |
183
|
|
|
* @param array $arguments |
184
|
|
|
* @param string $title |
185
|
|
|
*/ |
186
|
|
|
protected function addWarning($key, $code, array $arguments = [], $title = '') |
187
|
|
|
{ |
188
|
|
|
$message = $this->addMessage(Warning::class, $key, $code, $arguments, $title); |
189
|
|
|
$this->result->addWarning($message); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Creates a new validation notice and adds it to the result. |
194
|
|
|
* |
195
|
|
|
* @param string $key |
196
|
|
|
* @param int $code |
197
|
|
|
* @param array $arguments |
198
|
|
|
* @param string $title |
199
|
|
|
*/ |
200
|
|
|
protected function addNotice($key, $code, array $arguments = [], $title = '') |
201
|
|
|
{ |
202
|
|
|
$message = $this->addMessage(Notice::class, $key, $code, $arguments, $title); |
203
|
|
|
$this->result->addNotice($message); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $type |
208
|
|
|
* @param string $key |
209
|
|
|
* @param string $code |
210
|
|
|
* @param array $arguments |
211
|
|
|
* @param string $title |
212
|
|
|
* @return mixed |
213
|
|
|
* @throws \Exception |
214
|
|
|
*/ |
215
|
|
|
private function addMessage($type, $key, $code, array $arguments, $title) |
216
|
|
|
{ |
217
|
|
|
if (!isset($this->messages[$key])) { |
218
|
|
|
throw new \Exception('The error key "' . $key . '" does not exist for the validator "' . get_class($this) . '".', 1455272659); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return new $type( |
222
|
|
|
$this->getMessage($key, $arguments), |
223
|
|
|
$code, |
224
|
|
|
[], |
225
|
|
|
$title, |
226
|
|
|
AbstractFormValidator::getCurrentValidationName(), |
227
|
|
|
$key |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the full validation data. |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public function getValidationData() |
237
|
|
|
{ |
238
|
|
|
return $this->validationData; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Refreshes entirely the validation data (see `setValidationDataValue()`). |
243
|
|
|
* |
244
|
|
|
* @param array $validationData |
245
|
|
|
*/ |
246
|
|
|
protected function setValidationData(array $validationData) |
247
|
|
|
{ |
248
|
|
|
$this->validationData = array_merge($this->validationData, $validationData); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Adds an arbitral value to the validator, which will be added to the |
253
|
|
|
* `$validationData` property of the form. |
254
|
|
|
* |
255
|
|
|
* @param string $key Key of the data. |
256
|
|
|
* @param mixed $value Value bound to the key. |
257
|
|
|
*/ |
258
|
|
|
protected function setValidationDataValue($key, $value) |
259
|
|
|
{ |
260
|
|
|
$this->validationData[$key] = $value; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
public function getMessages() |
267
|
|
|
{ |
268
|
|
|
return $this->messages; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* This function should *always* be used when a message should be translated |
273
|
|
|
* when an error occurs in the validation process. |
274
|
|
|
* |
275
|
|
|
* You can also use `$this->getMessageFromKey()` if you want more |
276
|
|
|
* flexibility. |
277
|
|
|
* |
278
|
|
|
* @param string $key The key of the message, usually "default". |
279
|
|
|
* @param array $arguments Arguments given to the message. |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function getMessage($key, array $arguments = []) |
283
|
|
|
{ |
284
|
|
|
$result = (isset($this->messages[$key]['value']) && $this->messages[$key]['value'] !== '') |
285
|
|
|
? vsprintf($this->messages[$key]['value'], $arguments) |
286
|
|
|
: $this->getMessageFromKey($this->messages[$key]['key'], $this->messages[$key]['extension'], $arguments); |
287
|
|
|
|
288
|
|
|
list($result) = $this->signalSlotDispatcher->dispatch( |
289
|
|
|
__CLASS__, |
290
|
|
|
'getMessage', |
291
|
|
|
[$result, $this->messages[$key], $arguments] |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
return $result; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @see getMessage() |
299
|
|
|
* |
300
|
|
|
* @param string $key Path to the message. |
301
|
|
|
* @param string $extensionKey Extension containing the locallang reference to the message. |
302
|
|
|
* @param array $arguments Arguments given to the message. |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
protected function getMessageFromKey($key, $extensionKey = null, array $arguments = []) |
306
|
|
|
{ |
307
|
|
|
return Core::get()->translate($key, $extensionKey, $arguments); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
public static function getJavaScriptValidationFiles() |
314
|
|
|
{ |
315
|
|
|
return static::$javaScriptValidationFiles; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|