Answer the question
In order to leave comments, you need to log in
Checking if a field from metadata in a callback constraint is valid?
https://symfony.com/doc/current/reference/constrai...
public static function loadValidatorMetadata(ClassMetadataInterface $classMetadata): void {
// валидация полей
$classMetadata->addPropertyConstraints(
'email',
[
new Sequentially(
[
new NotBlank(),
....
]
),
]
);
$classMetadata->addConstraint(new Callback(['callback' => 'validate']));
}
/**
* @throws WrongInstanceOfClassException
* @throws BadParameterException
*/
public function validate(ExecutionContextInterface $context): void {
/** @var ClassMetadataInterface $classMetadata */
$classMetadata = $context->getMetadata();
if (false === $classMetadata instanceof ClassMetadataInterface) {
throw new WrongInstanceOfClassException();
}
// если поле валидно, добавить еще динамические констрейнты
if(true === $metadata->isValid('email')){
$metadata->addPropertyConstraints(
'email',
[
// дополнительные правила
]
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question