7

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.

jhogendorn
  • 5,931
  • 3
  • 26
  • 35
  • 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. – fazy Jan 18 '13 at 15:01
  • Does this look promising? https://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. – thormeier Feb 28 '14 at 17:30
  • Refs https://github.com/symfony/symfony/issues/20017 – Jacek Kobus Sep 21 '17 at 10:50

2 Answers2

1

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() })
 */
msg
  • 7,863
  • 3
  • 14
  • 33
-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.