草庐IT

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

coder 2024-01-02 原文

我一直在关注 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/

有关php - Symfony 2.5 addViolationAt 弃用,使用 buildViolation()的更多相关文章

  1. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  2. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  3. ruby - 有没有办法让 2.4.0 中的 Ruby 弃用警告静音? - 2

    从Ruby2.4.0开始,对于使用某些已弃用的功能,会出现弃用警告。例如,Bignum、Fixnum、TRUE和FALSE都会触发弃用警告。当我修复我的代码时,有相当多的代码我希望它保持沉默,尤其是在Rails中。我该怎么做? 最佳答案 moduleKerneldefsuppress_warningsoriginal_verbosity=$VERBOSE$VERBOSE=nilresult=yield$VERBOSE=original_verbosityreturnresultendend>>X=:foo=>:foo>>X=:bar

  4. ruby-on-rails - 从 gem 生成的静音弃用警告 - 2

    我正在使用unscoped_associations我的Rails5.0.0.1应用程序中的gem。我收到这个弃用警告:DEPRECATIONWARNING:alias_method_chainisdeprecated.Please,useModule#prependinstead.Frommodule,youcanaccesstheoriginalmethodusingsuper.(calledfromat/home/rhl/myapp/config/application.rb:8)DEPRECATIONWARNING:alias_method_chainisdeprecated.

  5. ruby-on-rails - Rails group_by 是否已弃用? - 2

    我有一个要分组的数组,“group_by”函数似乎适合我的情况。http://apidock.com/rails/Enumerable/group_by我在Rails3.2.13中使用它。grouped_array=my_array.group_by(&:my_function)#Assumerun'my_function'haveresult1onelement1,element3andresult2onelement2,element4,then:#grouped_array={#result1=>[element1,element3],#result2=>[element2,el

  6. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  7. ruby-on-rails - 验证事件连接!在 Rails 4 中已弃用,我们应该如何处理该功能? - 2

    我一直在关注这篇文章以与工头一起设置puma:https://www.digitalocean.com/community/articles/how-to-set-up-zero-downtime-rails-deploys-using-puma-and-foremanpuma脚本在连接后告诉verify_active_connections!但它在rails4中不可用。注释掉方法调用将使脚本运行但我不确定这是否会泄漏资源.关于这个问题,我能看到的唯一文档是:https://github.com/socialcast/resque-ensure-connected/issues/3但是

  8. ruby - 带 compass 的 ruby 弃用警告 - 2

    当我尝试像往常一样在项目上运行“bundleexeccompasswatch”时,我现在收到此警告:DEPRECATIONWARNINGonline87of/home/hedy/Sites/mywebsite.fr/src/vendor/bundle/ruby/2.3.0/gems/compass-core-1.0.3/stylesheets/compass/css3/_deprecated-support.scss:#{}interpolationnearoperatorswillbesimplifiedinafutureversionofSass.Topreservethecurr

  9. ruby - 通过 node-sass 的 Symfony assetic sass 过滤器? - 2

    我在让asseticsass过滤器与node-sass而不是ruby​​替代品一起工作时遇到了一些困难。我的config.yml文件中有以下配置:assetic:debug:"%kernel.debug%"use_controller:falsebundles:[]write-to:"%kernel.root_dir%/../web/assets"read_from:"%kernel.root_dir%/../web/assets"node:"%%PROGRAMFILES%%\nodejs\\node.exe"node_paths:["%%USERPROFILE%%\\AppData\

  10. ruby-on-rails - PHP 魔术方法 __call、__get 和 __set 的 Ruby 等价物 - 2

    我很确定Ruby有这些(等同于__call、__get和__set),否则find_by将如何在Rails中工作?也许有人可以举一个简单的例子来说明如何定义与find_by相同的方法?谢谢 最佳答案 简而言之你可以映射__调用带有参数的method_missing调用__设置为方法名称以'='结尾的method_missing调用__获取不带任何参数的method_missing调用__调用PHPclassMethodTest{publicfunction__call($name,$arguments){echo"Callingob

随机推荐