Symfony2:首次错误时验证停止

时间:2022-06-16 07:17:43

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.

好像是$ validator-> validate($ class);如果返回$ error类,Symfony2验证服务会运行所有验证检查。这通常没问题,但我正在寻找halt_on_failure功能,因为一旦某个特定属性失败,就会在正在进行的验证检查中触发PHP错误。

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.

在Symfony-1中有相当好的记录,有一个halt_on_error,但我不确定它在Symfony2中是如何工作的。我找到了它的参考但没有例子。

Here's a similar question

这是一个类似的问题

1 个解决方案

#1


1  

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:

    使用组。 Richard Miller写了一篇关于SF2验证与群组的完美摘要。基本上,我所做的是用Step1标记初始验证步骤,然后单独完成剩下的步骤。然后,您向Validator服务发出请求,如下所示:

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

    $ 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.

    额外的IF声明。在我的情况下,我使用API​​进行验证,所以我想确保输入是准确的,所以我没有浪费API请求。 IF语句可用于切断API请求并返回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.

我选择使用第2步。虽然第1步可能更好地使用了验证技术,但我并不认为需要添加的其他组参数。这是代码维护责任。在Symfony2.2中,似乎会有一个halt_on_error功能,此时应该更新此解决方案。

#1


1  

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:

    使用组。 Richard Miller写了一篇关于SF2验证与群组的完美摘要。基本上,我所做的是用Step1标记初始验证步骤,然后单独完成剩下的步骤。然后,您向Validator服务发出请求,如下所示:

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

    $ 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.

    额外的IF声明。在我的情况下,我使用API​​进行验证,所以我想确保输入是准确的,所以我没有浪费API请求。 IF语句可用于切断API请求并返回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.

我选择使用第2步。虽然第1步可能更好地使用了验证技术,但我并不认为需要添加的其他组参数。这是代码维护责任。在Symfony2.2中,似乎会有一个halt_on_error功能,此时应该更新此解决方案。