Symfony2:第一个错误时验证停止 [英] Symfony2 : Validation Halt on First Error
问题描述
似乎是 Symfony2 $validator->validate( $class );rel="nofollow noreferrer">验证服务 在返回 $error
类之前运行所有验证检查.这通常没问题,但我正在寻找 halt_on_failure
功能,因为一旦特定属性失败,它会在进行的验证检查中触发 PHP
错误.
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.
在 Symfony-1 中有相当详细的记录,有一个 halt_on_error
但我不确定它在 Symfony2 中是如何工作的.我找到了对它的引用,但没有例子.
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.
这是一个类似的问题
推荐答案
为这个问题找到了两个解决方案.
使用群组.理查德米勒写了一篇关于 SF2 Validation with Groups 的很好的总结.本质上,我所做的是标记初始验证步骤
Step1
并留下其余的步骤.然后,您可以像这样向 Validator 服务发出请求:
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'));
$this->validator->validate($class, array('step1', 'Default'));
其他 IF 语句.在我的情况下,我使用 API 进行验证,因此我想确保输入准确无误,以免浪费 API 请求.IF 语句可用于切断 API 请求并return false
.
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
.
我选择做什么以及为什么
我选择使用第 2 步.虽然第 1 步可能更好地利用了验证技术,但我对需要添加的其他组参数并不感冒.这是代码维护责任.看来在Symfony2.2中,会有一个halt_on_error
功能,这个解决方案应该到时候更新.
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.
这篇关于Symfony2:第一个错误时验证停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!