FormService::addFormWithErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
class FormService implements SingletonInterface
29
{
30
31
    /**
32
     * @var FormInterface[]
33
     */
34
    private static $formWithErrors = [];
35
36
    /**
37
     * If a form has been submitted with errors, use this function to get it
38
     * back.
39
     * This is useful because an action is forwarded if the submitted argument
40
     * has errors.
41
     *
42
     * @deprecated This method is deprecated, please try not to use it if you
43
     *             can. It will be removed in FormZ v2, where you will have a
44
     *             whole new way to get a validated form.
45
     *
46
     * @param string $formClassName The class name of the form.
47
     * @return FormInterface|null
48
     */
49
    public static function getFormWithErrors($formClassName)
50
    {
51
        GeneralUtility::logDeprecatedFunction();
52
53
        return (isset(self::$formWithErrors[$formClassName]))
54
            ? self::$formWithErrors[$formClassName]
55
            : null;
56
    }
57
58
    /**
59
     * If a form validation has failed, this function is used to save it for
60
     * further usage.
61
     *
62
     * @param FormInterface $form
63
     * @internal
64
     */
65
    public static function addFormWithErrors(FormInterface $form)
66
    {
67
        self::$formWithErrors[get_class($form)] = $form;
68
    }
69
}
70