Feature: #106945 - Allow usage of Symfony validators in Extbase
See forge#106945
Description
Extbase models and controllers now support the use of Symfony Validators. Validators are based on Symfony Constraints, which can be added as attributes to domain model properties and controller methods.
Once a constraint attribute is detected while reflecting properties or methods,
it is decorated by the new
Constraint
class, which is compatible with Extbase's
\Validator.
Decorated constraints may include localizable messages. If a message contains
valid LLL: syntax, the label will be translated automatically. The decorating
validator also handles message parameters by converting named parameters such as
{ into
sprintf-compatible placeholders like %1$s.
Important
Most available Symfony constraints, such as
# and
#, can be used. However, more complex constraints such as
# or
# are not yet compatible with the current
Extbase implementation, as they are closely tied to the Symfony framework.
Compatibility for those constraints may be added in the future.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Domain\Model;
use Symfony\Component\Validator\Constraints as Assert;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
class MyModel extends AbstractEntity
{
#[Assert\WordCount(max: 200, maxMessage: 'Biography must not exceed 200 words.')]
protected string $biography = '';
#[Assert\CssColor(message: 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:validator.avatarColor.error')]
protected string $avatarColor = '';
#[Assert\Iban]
protected string $iban = '';
public function getBiography(): string
{
return $this->biography;
}
public function setBiography(string $biography): void
{
$this->biography = $biography;
}
public function getAvatarColor(): string
{
return $this->avatarColor;
}
public function setAvatarColor(string $avatarColor): void
{
$this->avatarColor = $avatarColor;
}
public function getIban(): string
{
return $this->iban;
}
public function setIban(string $iban): void
{
$this->iban = $iban;
}
}
Impact
A wide range of Symfony validators can now be used directly in Extbase. This provides a more flexible and standardized validation workflow without the need to implement custom validators, as Symfony already ships with a large number of predefined constraints.