• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TYPO3-Headless / headless / 9856058913

09 Jul 2024 11:17AM UTC coverage: 65.849% (+0.07%) from 65.779%
9856058913

push

github

web-flow
[FEATURE] Allow to set validator only for backend in EXT:forms (#747)

When backendOnly=1 flag instructs default decorator to hide validator for frontend output

Example usage:
```
validators:
  - identifier: NotEmpty
    errorMessage: 1221560910
    backendOnly: 1
```

6 of 7 new or added lines in 1 file covered. (85.71%)

993 of 1508 relevant lines covered (65.85%)

2.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/Classes/Form/Decorator/AbstractFormDefinitionDecorator.php
1
<?php
2

3
/*
4
 * This file is part of the "headless" Extension for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.md file that was distributed with this source code.
8
 */
9

10
declare(strict_types=1);
11

12
namespace FriendsOfTYPO3\Headless\Form\Decorator;
13

14
use function in_array;
15

16
abstract class AbstractFormDefinitionDecorator implements DefinitionDecoratorInterface
17
{
18
    /**
19
     * @var array<string, mixed>
20
     */
21
    protected array $formStatus;
22
    protected string $formId = '';
23

24
    public function __construct(array $formStatus = [])
25
    {
26
        $this->formStatus = $formStatus;
3✔
27
    }
28

29
    /**
30
     * @param array<string, mixed> $definition
31
     * @return array<string,array<mixed>>
32
     */
33
    public function __invoke(array $definition, int $currentPage): array
34
    {
35
        $decorated = [];
2✔
36

37
        $pageElements = $definition['renderables'][$currentPage]['renderables'] ?? [];
2✔
38

39
        $this->formId = $definition['identifier'];
2✔
40

41
        $decorated['id'] = $this->formId;
2✔
42
        $decorated['api'] = $this->formStatus;
2✔
43
        $decorated['i18n'] = $definition['i18n']['properties'] ?? [];
2✔
44
        $decorated['elements'] = $this->handleRenderables($pageElements);
2✔
45

46
        return $this->overrideDefinition($decorated, $definition, $currentPage);
2✔
47
    }
48

49
    /**
50
     * @param array<string, mixed> $renderables
51
     * @return array<string, mixed>
52
     */
53
    protected function handleRenderables(array $renderables): array
54
    {
55
        foreach ($renderables as &$element) {
2✔
56
            if (in_array($element['type'], ['Fieldset', 'GridRow'], true) &&
1✔
57
                is_array($element['renderables'] ?? []) &&
1✔
58
                ($element['renderables'] ?? []) !== []) {
1✔
59
                $element['elements'] = $this->handleRenderables($element['renderables']);
1✔
60
                unset($element['renderables']);
1✔
61
            } else {
62
                $element = $this->prepareElement($element);
1✔
63
            }
64
        }
65

66
        return $renderables;
2✔
67
    }
68

69
    /**
70
     * @param array<string, mixed> $element
71
     * @return array<string, mixed>
72
     */
73
    protected function prepareElement(array $element): array
74
    {
75
        $element['name'] = 'tx_form_formframework[' . $this->formId . '][' . $element['identifier'] . ']';
1✔
76

77
        $element = $this->overrideElement($element);
1✔
78

79
        if (isset($element['renderingOptions']['FEOverrideType'])) {
1✔
80
            $element['type'] = $element['renderingOptions']['FEOverrideType'];
1✔
81
            unset($element['renderingOptions']['FEOverrideType']);
1✔
82
        }
83

84
        if (in_array($element['type'], ['ImageUpload', 'FileUpload'])) {
1✔
85
            unset($element['properties']['saveToFileMount']);
1✔
86
        }
87

88
        if (!isset($element['validators'])) {
1✔
89
            return $element;
1✔
90
        }
91

92
        $validators = [];
1✔
93

94
        foreach ($element['validators'] as $validator) {
1✔
95
            if ($validator['identifier'] === 'RegularExpression') {
1✔
96
                $jsRegex = $validator['FERegularExpression'] ?? null;
1✔
97

98
                if ($jsRegex) {
1✔
99
                    $validator['options']['regularExpression'] = $jsRegex;
1✔
100
                    unset($validator['FERegularExpression']);
1✔
101
                }
102
            }
103

104
            if ((int)($validator['backendOnly'] ?? 0)) {
1✔
NEW
105
                unset($validator);
×
106
            }
107

108
            if (isset($validator)) {
1✔
109
                $validators[] = $validator;
1✔
110
            }
111
        }
112

113
        $element['validators'] = $validators;
1✔
114

115
        return $element;
1✔
116
    }
117

118
    /**
119
     * @param array<string, mixed> $element
120
     * @return array<string, mixed>
121
     */
122
    protected function overrideElement(array $element): array
123
    {
124
        return $element;
1✔
125
    }
126

127
    /**
128
     * @param array<string, mixed> $decorated
129
     * @param array<string, mixed> $definition
130
     * @param int $currentPage
131
     * @return array<string, mixed>
132
     */
133
    protected function overrideDefinition(array $decorated, array $definition, int $currentPage): array
134
    {
135
        return $decorated;
2✔
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc