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\Middleware\Item\FormValidation; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
17
|
|
|
use Romm\Formz\Core\Core; |
18
|
|
|
use Romm\Formz\Middleware\Item\FieldValidation\FieldValidationArguments; |
19
|
|
|
use Romm\Formz\Middleware\Item\FieldValidation\FieldValidationSignal; |
20
|
|
|
use Romm\Formz\Middleware\Item\OnBeginMiddleware; |
21
|
|
|
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal; |
22
|
|
|
use Romm\Formz\Middleware\State\PresetMiddlewareInterface; |
23
|
|
|
use Romm\Formz\Middleware\State\RemoveFromSingleFieldValidationContext; |
24
|
|
|
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This middleware takes care of validating the form instance, with a proper |
28
|
|
|
* form validator instance. |
29
|
|
|
* |
30
|
|
|
* You can bind middlewares to the signal `FormValidationSignal`, which will be |
31
|
|
|
* dispatched if and only if the form was submitted by the user. |
32
|
|
|
* |
33
|
|
|
* Please note that this middleware will not be called when being in a "single |
34
|
|
|
* field validation context". |
35
|
|
|
* |
36
|
|
|
* @see \Romm\Formz\Middleware\State\RemoveFromSingleFieldValidationContext |
37
|
|
|
*/ |
38
|
|
|
class FormValidationMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var \Romm\Formz\Middleware\Item\FormValidation\FormValidationMiddlewareOption |
42
|
|
|
*/ |
43
|
|
|
protected $options; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var AbstractFormValidator |
47
|
|
|
*/ |
48
|
|
|
protected $validator; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @see FormValidationMiddleware |
52
|
|
|
*/ |
53
|
|
|
protected function process() |
54
|
|
|
{ |
55
|
|
|
$formObject = $this->getFormObject(); |
56
|
|
|
|
57
|
|
|
$this->beforeSignal(FormValidationSignal::class)->dispatch(); |
58
|
|
|
|
59
|
|
|
if ($formObject->hasForm() |
60
|
|
|
&& $formObject->formWasSubmitted() |
61
|
|
|
) { |
62
|
|
|
$this->validator = Core::instantiate( |
63
|
|
|
$this->options->getFormValidatorClassName(), |
64
|
|
|
['name' => $formObject->getName()] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->injectFieldValidationCallback(); |
68
|
|
|
$this->injectCurrentStep(); |
69
|
|
|
|
70
|
|
|
$this->validator->setInitialResult($this->getResult()); |
71
|
|
|
$this->validator->validate($formObject->getForm()); |
72
|
|
|
|
73
|
|
|
$this->afterSignal(FormValidationSignal::class)->dispatch(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @todo |
79
|
|
|
*/ |
80
|
|
|
protected function injectFieldValidationCallback() |
81
|
|
|
{ |
82
|
|
|
$callback = function (Field $field) { |
83
|
|
|
$this->afterSignal(FieldValidationSignal::class) |
84
|
|
|
->withArguments(new FieldValidationArguments($field)) |
85
|
|
|
->dispatch(); |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
$this->validator->getDataObject()->addFieldValidationCallback($callback); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @todo |
93
|
|
|
*/ |
94
|
|
|
protected function injectCurrentStep() |
95
|
|
|
{ |
96
|
|
|
$this->validator->getDataObject()->setCurrentStep($this->getCurrentStep()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getAllowedSignals() |
103
|
|
|
{ |
104
|
|
|
return [FormValidationSignal::class, FieldValidationSignal::class]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: