3

It seems the $validator->validate( $class ); of the Symfony2 Validation Service runs through all the validation checks before if returns the $error class. This is normally OK but I'm looking for halt_on_failure functionality because once a particular property fails it triggers a PHP error on a proceeding Validation check.

It's fairly well documented in Symfony-1 that there was a halt_on_error but I'm not sure how this works in Symfony2. I've found references to it but no examples.

Here's a similar question

Community
  • 1
  • 1
JustinP
  • 1,351
  • 1
  • 14
  • 29

1 Answers1

0

Found Two solutions for this problem.

  1. Using Groups. Richard Miller wrote a nice summary of SF2 Validation with Groups. Essentially, what I did was labelled the initial validation steps with Step1 and left the rest of the steps alone. You then make the request to the Validator service like this:

    $this->validator->validate($class, array('step1', 'Default'));

  2. Additional IF Statements. In my situation, I was using an API to validate so I wanted to make sure the inputs were accurate so I didn't waste an API request. IF statements can be used to cut off the API request and return false.

What I Choose to do and why

I choose to go with step 2. While, step 1 was probably better use of the validation technology, I wasn't wild about the additional group parameters that needed to be added. It was a code maintenance liability. It seems in Symfony2.2, there will be a halt_on_error feature and this solution should be updated at that time.

JustinP
  • 1,351
  • 1
  • 14
  • 29