M
M
Messi2021-12-10 18:11:29
symfony
Messi, 2021-12-10 18:11:29

How to set validation order with validation of another field in Symfony?

Hello! There is a conditional entity with two fields:

/**
     * @Assert\Range(min=1, max=self::SPEND_MAX)
     */
    public ?int $spendMax = null;

    /**
     * @Assert\NotBlank()
     * @Assert\Length(max=255)
     */
    public string $name;


How can I use annotations to make sure that before the Range validation for spendMax works, a check that the name field is not empty works?

Essentially, you need something like
/**
     * @Assert\NotBlank(<b>для поля name</b>)
     * @Assert\Range(min=1, max=self::SPEND_MAX)
     */
    public ?int $spendMax = null;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-12-13
@rpsv

- swap the fields
OR
- create 2 groups, put the fields in them and specify the order of the groups: https://symfony.com/doc/current/validation/sequenc...
OR
- instead of annotations, use the loadValidatorMetadata PHP method and specify the required one in it order: https://symfony.com/doc/current/validation.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question