在Symfony2中,可以使用导入将validation.yml文件拆分为多个文件吗?

时间:2012-02-22 21:47:53

标签: php symfony yaml

现在,我有一个名为validation.yml的文件,它在一个文件中验证了所有bundle的实体。

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

enquiry.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

8 个答案:

答案 0 :(得分:28)

load的{​​{1}}方法中添加这些行。

src/Blogger/BlogBundle/DependencyInjection/BloggerBlogExtension.php

答案 1 :(得分:7)

2015年增加了答案

  

从Symfony 2.7开始,XML和Yaml约束文件位于   加载了包的Resources/config/validation子目录   在2.7之前,只有Resources/config/validation.yml(或.xml)   加载。

更多信息:

答案 2 :(得分:6)

Symfony 2.5打破了上述解决方案。请参阅:https://stackoverflow.com/a/24210501/175753

答案 3 :(得分:5)

您可以添加到services.xml

<parameters>
    <parameter key="validator.mapping.loader.yaml_files_loader.mapping_files" type="collection">
        <parameter>Blogger\BlogBundle\Resources\config\comment.yml</parameter>
        <parameter>Blogger\BlogBundle\Resources\config\enquiry.yml</parameter>
    </parameter>
</parameters>

答案 4 :(得分:3)

另一种选择:

public function load(array $configs, ContainerBuilder $container)
{
    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

    $validatorFiles = $container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');

    $finder = new Finder();
    foreach ($finder->files()->in(__DIR__ . '/../Resources/config/validation') as $file) {
        $validatorFiles[] = $file->getRealPath();
    }
    $container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $validatorFiles);
}

这样,使用Finder组件,每次添加新的验证器文件时都不必担心触摸此文件。

答案 5 :(得分:2)

上述解决方案无法在Symfony 2.3中使用。

在2.3中,从加载器加载多个Yml文件更容易。例如:

    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('services.yml');
    $loader->load('services/menu.yml'); //new file to load

答案 6 :(得分:1)

这是YAML替代@MaksSlesarenko的答案。

parameters:
  validator.mapping.loader.yaml_files_loader.mapping_files:
    - "%kernel.root_dir%/../src/CompanyName/TestBundle/Resources/config/validation/Entity.DbObject.yml"

BTW有没有办法将%kernel.root_dir%/../src/CompanyName/TestBundle/更改为某些捆绑根变量(如%kernel.root_dir%)?

答案 7 :(得分:1)

自Symfony 3.3 起,YourBundle/Resources/config/validation/*中存在的所有文件均已注册。


由于Symfony 3.4 %kernel.project_dir%/config/validator/*中存在的所有文件也都被注册。