本文介绍了如何将参数传递给 Symfony2 中的验证约束 - 在 yml 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的应用程序添加一个包范围的参数,以便我可以将它添加到我的验证约束文件 (validation.yml) 中:

I am trying to add a bundle-wide parameter to my application so that I can add it to my Validation Constraint file (validation.yml):

myApp\myBundle\Entity\Contact:
properties:
    name:
      - NotBlank: { message: "%myvariable%" }

我通常在 config.yml 中添加我的参数:

I added my parameter normally in config.yml:

parameters:
    # Validation config
    myvariable: Please tell us your name.

但页面只呈现 %myvariable% 文本,而不是所需的字符串.我还希望在将验证消息添加到页面以在 JavaScript 中使用时在我的 FormBuilderInterface 中使用此参数.yml 允许吗?如果没有,我如何在更高级别包含这样的参数?

But the page just renders the %myvariable% text, rather than the desired string. I also wish to use this parameter in my FormBuilderInterface when adding the validation messages to the page for usage in JavaScript. Does yml allow this? If not, how do I include such a parameter at a higher level?

推荐答案

不,目前不可能.

它与 YAML 或 XML 甚至服务定义无关.Validator 组件自行读取验证规则——如您所见,其结构与服务定义完全不同.不幸的是,它没有替换约束中的参数.

It has nothing to do with YAML or XML or even service definitions. Validator component reads validation rules by itself - as you can see, the structure is quite different than for service definitions. Unfortunately, it does not replace the parameters in constraints.

主要逻辑位于 \Symfony\Component\Validator\Mapping\Loader\YamlFileLoader 中,由 \Symfony\Component\Validator\ValidatorBuilder::getValidator 创建.

The main logic resides in \Symfony\Component\Validator\Mapping\Loader\YamlFileLoader which is created by \Symfony\Component\Validator\ValidatorBuilder::getValidator.

您可以通过以下方式实现:

You could make this happen by:

  1. 覆盖 validator.builder 服务的定义.
  1. Overriding definition of validator.builder service.

它是使用 %validator.builder.factory.class%::createValidatorBuilder 构建的,但是由于您必须以某种方式获取参数包,因此没有足够的依赖项 - 正在使用类工厂,而不是服务工厂.

It's constructed using %validator.builder.factory.class%::createValidatorBuilder, but as you have to get parameter bag somehow, there is not enough dependencies - class factory is in use, not service factory.

  1. 创建新类,扩展 ValidatorBuilder.

它应该将参数包带入构造函数或通过setter.应该在步骤(1)中配置才能在这里传递.

It should take parameter bag into constructor or via setter. It should be configured in step (1) to be passed here.

这个类将创建另一个类的文件加载器(参见 3),并将该参数包传递给它.

This class would create file loaders of another class (see 3), also pass that parameter bag into it.

  1. YamlFileLoaderYamlFilesLoader 创建新类.对于您希望支持的每种格式,另外 2 个.
  1. Creating new classes for YamlFileLoader and YamlFilesLoader. Additional 2 for each format that you would want to support.

它还会将参数包带入构造函数并覆盖一些功能.例如,我认为所有参数处理都可以在 newConstraint 方法中完成 - 遍历选项,解析参数,然后使用替换选项调用父方法.

It would additionally take parameter bag into constructor and override some functionality. For example, I think all parameter handling could be done in newConstraint method - iterate through options, resolve parameters, then call parent method with replaced options.

很高兴 Symfony 可以像这样扩展(在这个用例中可能没有那么好),但我想用自定义约束验证器编写自己的约束会更容易,这会将该参数注入其中.

It's nice that Symfony could be extended like that (possibly not so nicely in this use-case), but I guess it would be easier to just write your own constraint with custom constraint validator, which would inject that parameter into it.

还要考虑一个围绕验证器服务的包装器 - 如果您只需要替换验证消息,您可以替换 validator 服务,将原始服务注入其中.参见 http://symfony.com/doc/current/service_container/service_decoration.html 了解更多信息.

Also consider a wrapper around validator service - if you just need to replace the validation messages, you could replace the validator service, injecting original one into it. See http://symfony.com/doc/current/service_container/service_decoration.html for more information.

这篇关于如何将参数传递给 Symfony2 中的验证约束 - 在 yml 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 03:18