.. include:: /Includes.rst.txt .. index:: Extbase; Validator .. _extbase_domain_validator: =================================== Validator =================================== .. seealso:: * :ref:`extbase_validation` for general validation in Extbase. Custom validators are located in the directory :file:`Classes/Domain/Validator` and therefore in the namespace :php:`Vendor\MyExtension\Domain\Validator`. All validators extend the :php:`AbstractValidator` (:php:`TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`). .. note:: In the package :php:`\TYPO3\CMS\Extbase\Validation\Validator\*` Extbase offers many validators for default requirements like the validation of emails, numbers and strings. You do not need to implement such basic checks yourself. Custom validator for a property of the domain model ==================================================== When the standard validators provided by Extbase are not sufficient you can write a custom validators to use on the property of a domain model: .. include:: /CodeSnippets/Extbase/Validator/PropertyValidator.rst.txt The method :php:`isValid()` does not return a value. In case of an error it adds an error to the validation result by calling method :php:`addError()`. The long number added as second parameter of this function is the current UNIX time in the moment the error message was first introduced. This way all errors can be uniquely identified. This validator can be used for any string property of model now by including it in the annotation of that parameter: .. include:: /CodeSnippets/Extbase/Validator/PropertyValidatorUsage.rst.txt .. note:: Validators added to a property of a model are executed whenever an object of that model is passed to a controller action as a parameter. The validation of the parameter can be disabled by the annotation :ref:`extbase-annotation-ignore-validation`. Complete domain model validation ================================= At certain times in the life cycle of a model it can be necessary to validate the complete domain model. This is usually done before calling a certain action that will persist the object. .. include:: /CodeSnippets/Extbase/Validator/ObjectValidator.rst.txt If the error is related to a specific property of the domain object, the function :php:`addErrorForProperty()` should be used instead of :php:`addError()`. The validator is used as annotation in the action methods of the controller: .. include:: /CodeSnippets/Extbase/Validator/ObjectValidatorUsage.rst.txt