【问题标题】:Symfony 2.5 addViolationAt deprecated, use buildViolation()Symfony 2.5 addViolationAt 已弃用,使用 buildViolation()
【发布时间】:2014-08-12 12:57:31
【问题描述】:

我一直在关注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:尝试调用方法“buildViolation” 类“Symfony\Component\Validator\ExecutionContext”在 剥离路径 第 23 行。您的意思是调用:“addViolation”吗?

第 23 行代码如下:

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

【问题讨论】:

    标签: php validation symfony


    【解决方案1】:

    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());
    

    【讨论】:

    • 我想我接受得太快了。目前我遇到以下错误: UndefinedMethodException: Attempted to call method "buildViolation" on class "Symfony\Component\Validator\ExecutionContext"
    • 我只能假设这是因为框架仍在使用“旧”上下文而不是 2.5+ 版本。 @deprecation 只是一个警告,它将在组件中删除,而我猜框架将按照自己的节奏工作。
    • 你是绝对正确的。我将以下内容添加到 config.yml framework.validation.api: 2.4 作为我之前遇到的另一个问题的“修复”,这让我意识到这个问题更多的是我的错误,而不是框架的问题。谢谢!
    • 很高兴,有点,帮助。
    猜你喜欢
    • 2016-08-18
    • 2015-06-18
    • 2023-04-02
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多