|
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\Core\Utility\GeneralUtility; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Contains features which can be useful in third party extensions. |
|
22
|
|
|
* |
|
23
|
|
|
* You can then use the following features in your scripts: |
|
24
|
|
|
* |
|
25
|
|
|
* - Access to the last submitted form which validation failed: |
|
26
|
|
|
* `$myFailedForm = FormUtility::getFormWithErrors(MyForm::class);` |
|
27
|
|
|
* |
|
28
|
|
|
* - Automatic redirect if required argument is missing: |
|
29
|
|
|
* |
|
30
|
|
|
* @see `onRequiredArgumentIsMissing()` |
|
31
|
|
|
*/ |
|
32
|
|
|
class FormService implements SingletonInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FormInterface[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $formWithErrors = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* If a form has been submitted with errors, use this function to get it |
|
42
|
|
|
* back. |
|
43
|
|
|
* This is useful because an action is forwarded if the submitted argument |
|
44
|
|
|
* has errors. |
|
45
|
|
|
* |
|
46
|
|
|
* @deprecated This method is deprecated, please try not to use it if you |
|
47
|
|
|
* can. It will be removed in FormZ v2, where you will have a |
|
48
|
|
|
* whole new way to get a validated form. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $formClassName The class name of the form. |
|
51
|
|
|
* @return FormInterface|null |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getFormWithErrors($formClassName) |
|
54
|
|
|
{ |
|
55
|
|
|
GeneralUtility::logDeprecatedFunction(); |
|
56
|
|
|
|
|
57
|
|
|
return (isset(self::$formWithErrors[$formClassName])) |
|
58
|
|
|
? self::$formWithErrors[$formClassName] |
|
59
|
|
|
: null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* If a form validation has failed, this function is used to save it for |
|
64
|
|
|
* further usage. |
|
65
|
|
|
* |
|
66
|
|
|
* @param FormInterface $form |
|
67
|
|
|
* @internal |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function addFormWithErrors(FormInterface $form) |
|
70
|
|
|
{ |
|
71
|
|
|
self::$formWithErrors[get_class($form)] = $form; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|