Skip to content

Commit d22052f

Browse files
linawolffroemkengarvinhicking
authored
[FEATURE] Provide PSR-7 request in Extbase validators (#5434)
* [FEATURE] Provide PSR-7 request in Extbase validators TYPO3-Documentation/Changelog-To-Doc#1209 will be documented in a follow up as this change needs to be backported to TYPO3 13. Resolves: TYPO3-Documentation/Changelog-To-Doc#938 Releases: main, 13.4 * Apply suggestions from code review Co-authored-by: Stefan Frömken <froemken@gmail.com> * Update Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Validator.rst * Update Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Validator.rst Co-authored-by: Stefan Frömken <froemken@gmail.com> * [FEATURE] Provide PSR-7 request in Extbase validators TYPO3-Documentation/Changelog-To-Doc#1209 will be documented in a follow up as this change needs to be backported to TYPO3 13. Resolves: TYPO3-Documentation/Changelog-To-Doc#938 Releases: main, 13.4 * Update Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Validator.rst Co-authored-by: Garvin Hicking <blog@garv.in> --------- Co-authored-by: Stefan Frömken <froemken@gmail.com> Co-authored-by: Garvin Hicking <blog@garv.in>
1 parent 7c2b7e1 commit d22052f

File tree

4 files changed

+84
-28
lines changed

4 files changed

+84
-28
lines changed

Documentation/ApiOverview/RequestLifeCycle/Typo3Request.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ The request object compatible with the PSR-7
6363
TYPO3 v10 and TYPO3 v11 you have to use the :ref:`global variable
6464
<typo3-request-global-variable>`.
6565

66+
.. _typo3-request-extbase-validator:
67+
68+
Extbase validator
69+
-----------------
70+
71+
.. versionadded:: 13.2
72+
Extbase :php-short:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`
73+
provides a getter and a setter for the PSR-7 Request object.
74+
75+
In Extbase validators the current request is available with
76+
`$this->getRequest()` if they extend the :php-short:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`:
77+
78+
.. literalinclude:: /ExtensionArchitecture/Extbase/Reference/Domain/_Validator/_RequestValidator.php
79+
:caption: EXT:my_extension/Classes/Domain/Validators/MyCustomValidator.php
80+
6681
.. _typo3-request-viewhelper:
6782

6883
ViewHelper
Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
.. include:: /Includes.rst.txt
1+
:navigation-title: Validator
22

3-
.. index:: Extbase; Validator
3+
.. include:: /Includes.rst.txt
4+
.. index:: Extbase; Validator
5+
.. _extbase_domain_validator:
46

5-
.. _extbase_domain_validator:
7+
=========================
8+
Custom Extbase Validators
9+
=========================
610

7-
=========
8-
Validator
9-
=========
10-
11-
.. seealso::
12-
* :ref:`extbase_validation` for general validation in Extbase.
11+
.. seealso::
12+
* :ref:`extbase_validation` for general validation in Extbase.
1313

1414
Custom validators are located in the directory :file:`Classes/Domain/Validator`
15-
and therefore in the namespace :php:`Vendor\MyExtension\Domain\Validator`.
15+
and therefore in the namespace :php:`MyVendor\MyExtension\Domain\Validator`.
16+
17+
All validators must implement :php-short:`TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface`.
18+
They usually extend the :php-short:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`.
1619

17-
All validators extend the :php:`AbstractValidator`
18-
(:php:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`).
20+
.. note::
21+
In the directory :php:`\TYPO3\CMS\Extbase\Validation\Validator\*` Extbase
22+
offers many validators for default requirements like the validation of
23+
emails, numbers and strings. You do not need to implement such basic
24+
checks yourself.
1925

20-
.. note::
21-
In the package :php:`\TYPO3\CMS\Extbase\Validation\Validator\*` Extbase
22-
offers many validators for default requirements like the validation of
23-
emails, numbers and strings. You do not need to implement such basic
24-
checks yourself.
26+
.. _extbase_domain_validator-model:
2527

2628
Custom validator for a property of the domain model
2729
====================================================
2830

2931
When the standard validators provided by Extbase are not sufficient you can
3032
write a custom validators to use on the property of a domain model:
3133

32-
.. include:: /CodeSnippets/Extbase/Validator/PropertyValidator.rst.txt
34+
.. include:: /CodeSnippets/Extbase/Validator/PropertyValidator.rst.txt
3335

3436
The method :php:`isValid()` does not return a value. In case of an error it
3537
adds an error to the validation result by calling method :php:`addError()`.
@@ -43,12 +45,14 @@ in the annotation of that parameter:
4345
.. literalinclude:: _CustomValidator/_PropertyValidatorUsage.php
4446
:caption: EXT:blog_example/Classes/Domain/Model/Blog.php, modified
4547

46-
.. note::
47-
Validators added to a property of a model are executed whenever an object
48-
of that model is passed to a controller action as a parameter.
48+
.. note::
49+
Validators added to a property of a model are executed whenever an object
50+
of that model is passed to a controller action as a parameter.
51+
52+
The validation result of the parameter can be ignored by using the annotation
53+
:ref:`extbase-annotation-ignore-validation`.
4954

50-
The validation of the parameter can be disabled by the annotation
51-
:ref:`extbase-annotation-ignore-validation`.
55+
.. _extbase_domain_validator-model-complete:
5256

5357
Complete domain model validation
5458
=================================
@@ -57,7 +61,7 @@ At certain times in the life cycle of a model it can be necessary to validate
5761
the complete domain model. This is usually done before calling a certain action
5862
that will persist the object.
5963

60-
.. include:: /CodeSnippets/Extbase/Validator/ObjectValidator.rst.txt
64+
.. include:: /CodeSnippets/Extbase/Validator/ObjectValidator.rst.txt
6165

6266
If the error is related to a specific property of the domain object, the
6367
function :php:`addErrorForProperty()` should be used instead of :php:`addError()`.
@@ -67,12 +71,28 @@ The validator is used as annotation in the action methods of the controller:
6771
.. literalinclude:: _CustomValidator/_ObjectValidatorUsage.php
6872
:caption: EXT:blog_example/Classes/Controller/BlogController.php, modified
6973

74+
.. _extbase_domain_validator-di:
75+
7076
Dependency injection in validators
7177
==================================
7278

73-
Starting with TYPO3 v12 Extbase validators are capable of :ref:`dependency injection <Dependency-Injection>`
79+
Extbase validators are capable of :ref:`dependency injection <Dependency-Injection>`
7480
without further configuration, you can use the constructor method:
7581

7682
.. literalinclude:: _Validator/_MyCustomValidator.php
77-
:language: php
78-
:caption: EXT:my_extension/Classes/Validators/MyCustomValidator.php
83+
:caption: EXT:my_extension/Classes/Domain/Validators/MyCustomValidator.php
84+
85+
.. _extbase_domain_validator-request:
86+
87+
Request object in Extbase validators
88+
====================================
89+
90+
.. versionadded:: 13.2
91+
Extbase :php-short:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`
92+
provides a getter and a setter for the PSR-7 Request object.
93+
94+
You can use the PSR-7 request object in a validator, for example to get
95+
the site settings:
96+
97+
.. literalinclude:: _Validator/_RequestValidator.php
98+
:caption: EXT:my_extension/Classes/Domain/Validators/MyCustomValidator.php

Documentation/ExtensionArchitecture/Extbase/Reference/Domain/_Validator/_MyCustomValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
namespace MyVendor\MyExtension\Validators;
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Domain\Validators;
46

57
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
68

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Domain\Validators;
6+
7+
use TYPO3\CMS\Core\Site\Entity\Site;
8+
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
9+
10+
final class MyCustomValidator extends AbstractValidator
11+
{
12+
protected function isValid(mixed $value): void
13+
{
14+
/** @var ?Site $site */
15+
$site = $this->getRequest()?->getAttribute('site');
16+
$siteSettings = $site?->getSettings() ?? [];
17+
// TODO: Implement isValid() method using site settings
18+
}
19+
}

0 commit comments

Comments
 (0)