在Symfony2中,可以使用导入将validation.yml文件拆分为多个文件吗? [英] In Symfony2, can the validation.yml file be split into multiple files using imports?

查看:83
本文介绍了在Symfony2中,可以使用导入将validation.yml文件拆分为多个文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Right now, I have a file called validation.yml with the validation of all the bundle's entities in one file.

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

但是我想将其拆分为两个文件,然后将它们都导入.这是我尝试过的方法,但是没有用:

But I'd like to split it into two files and import them both. This is what I tried and it didn't work:

validation.yml

validation.yml

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

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

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

推荐答案

src/Blogger/BlogBundle/DependencyInjection/BloggerBlogExtension.phpload方法中添加这些行.

Add these lines in load method of src/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);
}

这篇关于在Symfony2中,可以使用导入将validation.yml文件拆分为多个文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆