How to stop validation on constraint failure in Symfony2
Asked Answered
N

2

9

If I have many validators against my entity, can I somehow specify one that it stops the rest if it fails? IE: there's no point checking Permissions if it fails NotBlank.

Alternatively, if its not built in, perhaps theres a way to signal the graph walker to stop, and I can put in a validator that checks for prior failures and stops propagation through the graph.

Neela answered 13/4, 2012 at 4:45 Comment(3)
I would appreciate knowing the answer to this as well. For me, in the NotBlank case, HTML5 prevents the user from seeing unnecessary errors, but I expect I will soon run into a more complex case where I have two or more validators besides NotBlank.Signore
Does this look promising? gist.github.com/rybakit/4705749 You're able to chain validators and have a flag $stopOnError. You probably need to change it a bit, because it's written for Symfony2.1.Enshroud
Refs github.com/symfony/symfony/issues/20017Commodus
R
4

Despite this being quite old, as of 5.1 there is a way to do this by using the Sequentially constraint.

This constraint allows you to apply a set of rules that should be validated step-by-step, allowing to interrupt the validation once the first violation is raised.

You would just pass an array of the constraints to validate:

/**
 * @Assert\Sequentially({ @Assert\NotBlank(), @Permissions() })
 */
Rau answered 25/10, 2020 at 8:50 Comment(0)
W
-1

If you set the validation in ./app/config/validation.yml then SF2 will validate as the order of validations in the file. Once a validation is failed, it ignore the others.

Wombat answered 7/3, 2014 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.