Validator

See also

Custom validators are located in the directory Classes/Domain/Validator and therefore in the namespace Vendor\MyExtension\Domain\Validator.

All validators extend the AbstractValidator (TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator).

Note

In the package \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:

EXT:blog_example/Classes/Domain/Validator/TitleValidator.php
final class TitleValidator extends AbstractValidator
{
    protected function isValid(mixed $value): void
    {
        // $value is the title string
        if (str_starts_with('_', $value)) {
            $errorString = 'The title may not start with an underscore. ';
            $this->addError($errorString, 1297418976);
        }
    }
}

The method isValid() does not return a value. In case of an error it adds an error to the validation result by calling method 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:

EXT:blog_example/Classes/Domain/Model/Blog.php
class Blog extends AbstractEntity
{
    /**
     * @Extbase\Validate("FriendsOfTYPO3\BlogExample\Domain\Validator\TitleValidator")
     */
    protected string $title = '';
}

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 IgnoreValidation.

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.

EXT:blog_example/Classes/Domain/Validator/BlogValidator.php
use FriendsOfTYPO3\BlogExample\Domain\Model\Blog;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

final class BlogValidator extends AbstractValidator
{
    protected function isValid(mixed $value): void
    {
        if (!$value instanceof Blog) {
            $errorString = 'The blog validator can only handle classes '
                . 'of type FriendsOfTYPO3\BlogExample\Domain\Validator\Blog. '
                . $value::class . ' given instead.';
            $this->addError($errorString, 1297418975);
        }
        if ($value->getCategories()->count() > 3) {
            $errorString = LocalizationUtility::translate(
                'error.Blog.tooManyCategories',
                'BlogExample'
            );
            // Add the error to the property if it is specific to one property
            $this->addErrorForProperty('categories', $errorString, 1297418976);
        }
        if (strtolower($value->getTitle()) === strtolower($value->getSubtitle())) {
            $errorString = LocalizationUtility::translate(
                'error.Blog.invalidSubTitle',
                'BlogExample'
            );
            // Add the error directly if it takes several properties into account
            $this->addError($errorString, 1297418974);
        }
    }
}

If the error is related to a specific property of the domain object, the function addErrorForProperty() should be used instead of addError().

The validator is used as annotation in the action methods of the controller:

EXT:blog_example/Classes/Controller/BlogController.php
use FriendsOfTYPO3\BlogExample\Domain\Model\Blog;
use FriendsOfTYPO3\BlogExample\Exception\NoBlogAdminAccessException;
use Psr\Http\Message\ResponseInterface;

class BlogController extends AbstractController
{
    /**
     * Updates an existing blog
     *
     * $blog is a not yet persisted clone of the original blog containing
     * the modifications
     *
     * @Extbase\Validate(param="blog", validator="FriendsOfTYPO3\ExtbaseExample\Domain\Validator\BlogValidator")
     * @throws NoBlogAdminAccessException
     */
    public function updateAction(Blog $blog): ResponseInterface
    {
        $this->checkBlogAdminAccess();
        $this->blogRepository->update($blog);
        $this->addFlashMessage('updated');
        return $this->redirect('index');
    }
}