B
B
BonBon Slick2021-07-30 01:18:20
symfony
BonBon Slick, 2021-07-30 01:18:20

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',
            [
               // дополнительные правила 
            ]
        );
}

Is there a solution out of the box or a hack?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question