如何将参数传递到Symfony2 - in yml中的验证约束?

时间:2023-01-15 10:45:29

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):

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

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

I added my parameter normally in config.yml:

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

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

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?

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

1 个解决方案

#1


1  

No, it's not currently possible.

不,目前不可能。

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.

它与YAML、XML甚至服务定义都没有关系。Validator组件自己读取验证规则——您可以看到,其结构与服务定义非常不同。不幸的是,它不能替换约束中的参数。

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

主要的逻辑位于\Symfony\组件验证器中,它是由\Symfony\组件\验证器:getValidator创建的。

You could make this happen by:

你可以这样做:

  1. Overriding definition of validator.builder service.
  2. 压倒一切的验证器的定义。建筑服务。

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.

这是使用% validator.builder.factory构造。类%:createValidatorBuilder,但是由于您必须以某种方式获得参数包,所以没有足够的依赖关系—正在使用类工厂,而不是服务工厂。

  1. Creating new class, which extends ValidatorBuilder.
  2. 创建新类,扩展ValidatorBuilder。

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

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

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

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

  1. Creating new classes for YamlFileLoader and YamlFilesLoader. Additional 2 for each format that you would want to support.
  2. 为YamlFileLoader和YamlFilesLoader创建新类。每一种你想要支持的格式增加2个。

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.

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


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.

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

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.

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

#1


1  

No, it's not currently possible.

不,目前不可能。

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.

它与YAML、XML甚至服务定义都没有关系。Validator组件自己读取验证规则——您可以看到,其结构与服务定义非常不同。不幸的是,它不能替换约束中的参数。

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

主要的逻辑位于\Symfony\组件验证器中,它是由\Symfony\组件\验证器:getValidator创建的。

You could make this happen by:

你可以这样做:

  1. Overriding definition of validator.builder service.
  2. 压倒一切的验证器的定义。建筑服务。

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.

这是使用% validator.builder.factory构造。类%:createValidatorBuilder,但是由于您必须以某种方式获得参数包,所以没有足够的依赖关系—正在使用类工厂,而不是服务工厂。

  1. Creating new class, which extends ValidatorBuilder.
  2. 创建新类,扩展ValidatorBuilder。

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

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

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

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

  1. Creating new classes for YamlFileLoader and YamlFilesLoader. Additional 2 for each format that you would want to support.
  2. 为YamlFileLoader和YamlFilesLoader创建新类。每一种你想要支持的格式增加2个。

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.

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


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.

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

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.

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