Symfony 2.5 addViolationAt deprecated, use buildViolation()

后端 未结 1 1730
不思量自难忘°
不思量自难忘° 2021-01-05 06:44

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(

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 07:11

    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.

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

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

    0 讨论(0)
提交回复
热议问题