Search code examples
annotationstypo3customvalidator

TYPO3: how to create a validator for field values when record is stored in the DB


I am looking for a way to validate a property of my Domain Model each time the record is stored in the database.

I had a look to the documentation on "Validation" and also on "Annotation", but was not able to get it working.

My model has a property defined:


    /**
     * vcFilename
     *
     * @var string
     * @Validate("Averlon\AvVcard\Domain\Validator\VcFilenameValidator")
     */
    protected string $vcFilename = '';

I have created a Validator which looks like this (at the moment):


class VcFilenameValidator extends AbstractValidator
{
    protected function isValid(mixed $value): void
    {
        echo '<pre>';
        echo '$value' . '<br>';
        var_dump($value);
        echo '</pre>';
        echo '<br>';

        die();
    }

But when storing a record in a folder in my BE nothing dies!

Normally, when I do this for testing purpose in other areas I get the output from var_dump in my BE.

What could be wrong?


Solution

  • Extbase validators are not meant for the TYPO3 backend but only for the frontend. If you want to validate the backend input you have two options:

    1. The first one is to extend the 'eval' configuration using https://docs.typo3.org/m/typo3/reference-tca/11.5/en-us/ColumnsConfig/Type/Input/Properties/Eval.html#custom-eval-rules

    2. There are many hooks in the datahandler which is used to store the records in the backend. Here you could validate the input before or after saving https://github.com/TYPO3/typo3/blob/66e6210e079c54f52253bb53c805673acefe6abc/typo3/sysext/core/Classes/DataHandling/DataHandler.php#L1124