Disable validation for single model property in TYPO3

In this blog post, I explain how can you disable validation for specific model properties in specific actions.

This one is a quick blog post about solution to a problem which I faced recently.

The Problem

If you have worked with TYPO3 validations, you know that validations are associated to properties of a model.

For example: You have a your model and the user model has a $email property.

    /**
     * @var string
     * @validate NotEmpty
     * @validate EmailAddress
     */
    protected $email = '';

This works, when all properties are present in an update request. But let’s say you have got a form where a user updates all fields except for the $email.

Unfortunately, it is not that easy in Extbase to exclude specific actions from validation.

The Solution

There are a few solutions, but my favorite one is removing validators in initialize*Action.

The initialize*Action

The initialize*Action is a specific convention in TYPO3. If you have an updateAction, you can write an initializeUpdateAction method. This methods gets executed before the updateAction action.

We use the initializeUpdateAction to remove validators from specific properties.

 public function initializeUpdateProfileAction() {
        /** @var ConjunctionValidator $validator */
        $validator = $this->arguments
            ->getArgument('user')
            ->getValidator();

        foreach ($validator->getValidators() as $subValidator) {
            /** @var GenericObjectValidator $subValidatorSub */
            foreach ($subValidator->getValidators() as $subValidatorSub) {
                $subValidatorSub->getPropertyValidators('email')->removeAll(
                    $subValidatorSub->getPropertyValidators('email')
                );
            }
        }
    }

The example above removes validation of $email property from User model.