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\Service; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Form\FormInterface; |
17
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
18
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments; |
19
|
|
|
use TYPO3\CMS\Extbase\Mvc\Request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Contains features which can be useful in third party extensions. |
23
|
|
|
* |
24
|
|
|
* You can then use the following features in your scripts: |
25
|
|
|
* |
26
|
|
|
* - Access to the last submitted form which validation failed: |
27
|
|
|
* `$myFailedForm = FormUtility::getFormWithErrors(MyForm::class);` |
28
|
|
|
* |
29
|
|
|
* - Automatic redirect if required argument is missing: |
30
|
|
|
* |
31
|
|
|
* @see `onRequiredArgumentIsMissing()` |
32
|
|
|
*/ |
33
|
|
|
class FormService implements SingletonInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var FormInterface[] |
38
|
|
|
*/ |
39
|
|
|
private static $formWithErrors = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Use this function to check if an action's required argument is missing. |
43
|
|
|
* |
44
|
|
|
* Basically, this can be used to prevent user from refreshing a page where |
45
|
|
|
* a form has been submitted, but without sending the form again: the |
46
|
|
|
* request calls the form submission controller action with an empty form |
47
|
|
|
* object, which can result in a fatal error. You can use this function to |
48
|
|
|
* prevent this kind of behaviour. Example: |
49
|
|
|
* |
50
|
|
|
* A controller has an action like: |
51
|
|
|
* public function submitFormAction(MyForm $myForm) { ... } |
52
|
|
|
* |
53
|
|
|
* Add a new function: |
54
|
|
|
* public function initializeSubmitFormAction() |
55
|
|
|
* { |
56
|
|
|
* FormUtility::onRequiredArgumentIsMissing( |
57
|
|
|
* $this->arguments, |
58
|
|
|
* $this->request, |
59
|
|
|
* function($missingArgumentName) { |
60
|
|
|
* $this->redirect('myIndex'); |
61
|
|
|
* } |
62
|
|
|
* ); |
63
|
|
|
* } |
64
|
|
|
* |
65
|
|
|
* @param Arguments $arguments Arguments of the method arguments. |
66
|
|
|
* @param Request $request Request. |
67
|
|
|
* @param callable $callback Callback function which is called if a required argument is missing. |
68
|
|
|
*/ |
69
|
|
|
public static function onRequiredArgumentIsMissing($arguments, $request, $callback) |
70
|
|
|
{ |
71
|
|
|
foreach ($arguments as $argument) { |
72
|
|
|
$argumentName = $argument->getName(); |
73
|
|
|
if (false === $request->hasArgument($argumentName) |
74
|
|
|
&& true === $argument->isRequired() |
75
|
|
|
) { |
76
|
|
|
if (is_callable($callback)) { |
77
|
|
|
$callback($argument->getName()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* If a form has been submitted with errors, use this function to get it |
85
|
|
|
* back. |
86
|
|
|
* This is useful because an action is forwarded if the submitted argument |
87
|
|
|
* has errors. |
88
|
|
|
* |
89
|
|
|
* @param string $formClassName The class name of the form. |
90
|
|
|
* @return FormInterface|null |
91
|
|
|
*/ |
92
|
|
|
public static function getFormWithErrors($formClassName) |
93
|
|
|
{ |
94
|
|
|
return (isset(self::$formWithErrors[$formClassName])) |
95
|
|
|
? self::$formWithErrors[$formClassName] |
96
|
|
|
: null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* If a form validation has failed, this function is used to save it for |
101
|
|
|
* further usage. |
102
|
|
|
* |
103
|
|
|
* @param FormInterface $form |
104
|
|
|
* @internal |
105
|
|
|
*/ |
106
|
|
|
public static function addFormWithErrors(FormInterface $form) |
107
|
|
|
{ |
108
|
|
|
self::$formWithErrors[get_class($form)] = $form; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|