在 Symfony2 中,可以使用导入将 validation.yml 文件拆分为多个文件吗?2015 年添加的答案

2022-08-30 21:36:44

现在,我有一个名为 validation.yml 的文件,其中验证了捆绑包的所有实体在一个文件中。

validation.yml

Blogger\BlogBundle\Entity\Comment
    properties:
        username:
            - NotBlank:
                message: You must enter your name
            - MaxLength: 50
        comment:
            - NotBlank:
                message: You must enter a comment
            - MinLength: 50

Blogger\BlogBundle\Entity\Enquiry:
    properties:
        name:
            - NotBlank: ~
        email:
            - Email:
                message: symblog does not like invalid emails. Give me a real one!
        subject:
            - NotBlank: ~
            - MaxLength: 50
        body:
            - MinLength: 50

但我想把它分成两个文件并同时导入它们。这是我尝试过的,但它不起作用:

validation.yml

imports:
    - { resource: comment.yml }
    - { resource: enquiry.yml }

comment.yml

Blogger\BlogBundle\Entity\Comment
    properties:
        username:
            - NotBlank:
                message: You must enter your name
            - MaxLength: 50
        comment:
            - NotBlank:
                message: You must enter a comment
            - MinLength: 50

查询.yml

Blogger\BlogBundle\Entity\Enquiry:
    properties:
        name:
            - NotBlank: ~
        email:
            - Email:
                message: symblog does not like invalid emails. Give me a real one!
        subject:
            - NotBlank: ~
            - MaxLength: 50
        body:
            - MinLength: 50

答案 1

在 方法中添加这些行。loadsrc/Blogger/BlogBundle/DependencyInjection/BloggerBlogExtension.php

public function load(array $configs, ContainerBuilder $container)
{
  //...
  $yamlMappingFiles = $container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');
  $yamlMappingFiles[] = __DIR__.'/../Resources/config/comment.yml';
  $yamlMappingFiles[] = __DIR__.'/../Resources/config/enquiry.yml';
  $container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $yamlMappingFiles);
}

答案 2

2015 年添加的答案

从 Symfony 开始,位于捆绑包子目录中的 XML 和 Yaml 约束文件被加载。
在此之前,仅加载了(或.xml)。2.7Resources/config/validation2.7Resources/config/validation.yml

更多信息请见:


推荐