Symfony 2.5 addViolation在不建议使用的情况下,请使用buildViolation() [英] Symfony 2.5 addViolationAt deprecated, use buildViolation()
问题描述
我一直在关注烹饪书关于如何创建类约束验证器的信息,现在我正要在validate()
函数中添加违规的地方.
I have been following the cookbook on how to create a class constraint validator and right now I am at the point where I am about to add the violation in the validate()
function.
但是我的IDE通知我功能addViolation()
和addViolationAt()
已弃用.
However my IDE notifies me the functions addViolation()
and addViolationAt()
are deprecated.
有人可以向我指出如何使用 Context\ExecutionContextInterface::buildViolation()
函数?
Can someone point me in the right direction on how to use the Context\ExecutionContextInterface::buildViolation()
function instead?
$this->context
是 Symfony\Component\Validator\ExecutionContext
的实例
class ProtocolClassValidator extends ConstraintValidator
{
public function validate($protocol, Constraint $constraint)
{
if ($protocol->getFoo() != $protocol->getBar()) {
$this->context->addViolationAt(
'foo',
$constraint->message,
array(),
null
);
}
}
}
将呼叫$this->context->addViolationAt()
更改为简单的$this->context->buildViolation()
时,出现以下异常:
When changing the call $this->context->addViolationAt()
to simply $this->context->buildViolation()
, I get the following Exception:
UndefinedMethodException:尝试在以下位置调用方法"buildViolation" 类别中的"Symfony \ Component \ Validator \ ExecutionContext" 剥离路径 第23行.您是说要打电话给"addViolation"吗?
UndefinedMethodException: Attempted to call method "buildViolation" on class "Symfony\Component\Validator\ExecutionContext" in stripped path line 23. Did you mean to call: "addViolation"?
第23行包含以下代码:
and line 23 has the following code:
$builder = $this->context->buildViolation($constraint->message)
->atPath('formField')
->addViolation();
推荐答案
addViolation
和addViolationAt
从2.5弃用,但直到3.0才被删除,因此它们仍然可以使用很长时间.
addViolation
and addViolationAt
are deprecated from 2.5 but won't be removed until 3.0 so they are still usable for a good time.
但是...取自从2.x升级到3.0 记录...
However... taken from the the UPGRADE FROM 2.x to 3.0 log...
The method `addViolationAt()` was removed. You should use `buildViolation()`
instead.
Before:
$context->addViolationAt('property', 'The value {{ value }} is invalid.', array(
'{{ value }}' => $invalidValue,
));
After:
$context->buildViolation('The value {{ value }} is invalid.')
->atPath('property')
->setParameter('{{ value }}', $invalidValue)
->addViolation();
));
更多内容摘自 Context/ExecutionContextInterface ...
With a bit more taken from Context/ExecutionContextInterface...
/**
* Returns a builder for adding a violation with extended information.
*
* Call {@link ConstraintViolationBuilderInterface::addViolation()} to
* add the violation when you're done with the configuration:
*
* $context->buildViolation('Please enter a number between %min% and %max.')
* ->setParameter('%min%', 3)
* ->setParameter('%max%', 10)
* ->setTranslationDomain('number_validation')
* ->addViolation();
*
* @param string $message The error message
* @param array $parameters The parameters substituted in the error message
*
* @return ConstraintViolationBuilderInterface The violation builder
*/
public function buildViolation($message, array $parameters = array());
这篇关于Symfony 2.5 addViolation在不建议使用的情况下,请使用buildViolation()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!