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.
/**
* @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.