gpt4 book ai didi

php - Symfony 2.5 addViolationAt 弃用,使用 buildViolation()

转载 作者:可可西里 更新时间:2023-11-01 12:42:49 25 4
gpt4 key购买 nike

我一直在关注 cookbook关于如何创建类约束验证器,现在我正准备在 validate() 函数中添加违规。

但是我的 IDE 通知我函数 addViolation()addViolationAt() 已被弃用。

有人可以为我指明如何使用 Context\ExecutionContextInterface::buildViolation() 的正确方向吗?函数代替?

$this->contextSymfony\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() 时,我得到以下异常:

UndefinedMethodException: Attempted to call method "buildViolation" on class "Symfony\Component\Validator\ExecutionContext" in stripped path line 23. Did you mean to call: "addViolation"?

第23行代码如下:

    $builder = $this->context->buildViolation($constraint->message)
->atPath('formField')
->addViolation();

最佳答案

addViolationaddViolationAt 已从 2.5 中弃用,但在 3.0 之前不会被删除,因此它们仍然可以使用一段时间。

然而...取自UPGRADE FROM 2.x to 3.0日志...

addViolationAt() 方法已被删除。您应该改用 buildViolation():

之前:

$context->addViolationAt('property', 'The value {{ value }} is invalid.', array(
'{{ value }}' => $invalidValue,
));

之后:

$context->buildViolation('The value {{ value }} is invalid.')
->atPath('property')
->setParameter('{{ value }}', $invalidValue)
->addViolation();
));

更多内容来自 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());

关于php - Symfony 2.5 addViolationAt 弃用,使用 buildViolation(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25264922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com