In Symfony2, can the validation.yml file be split into multiple files using imports?

前端 未结 8 1089
旧巷少年郎
旧巷少年郎 2020-12-13 10:48

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

validation.yml

Blogger\\BlogBundle\\Ent         


        
相关标签:
8条回答
  • 2020-12-13 11:02

    This is YAML alternative to @MaksSlesarenko answer.

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

    BTW is there any way to change %kernel.root_dir%/../src/CompanyName/TestBundle/ to some bundle root variable (like %kernel.root_dir%)?

    0 讨论(0)
  • 2020-12-13 11:03

    Another alternative:

    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);
    }
    

    This way, using the Finder Component, you don't have to be concerned about touching this file each time you add a new validator file.

    0 讨论(0)
  • 2020-12-13 11:14

    Solutions above are not working in Symfony 2.3.

    In 2.3 it's easier to load multiple Yml files from loader. For example:

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
        $loader->load('services/menu.yml'); //new file to load
    
    0 讨论(0)
  • 2020-12-13 11:19

    Symfony 2.5 broke the above solutions. See: https://stackoverflow.com/a/24210501/175753

    0 讨论(0)
  • 2020-12-13 11:21

    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);
    }
    
    0 讨论(0)
  • 2020-12-13 11:23

    Answer added at 2015

    As of Symfony 2.7, XML and Yaml constraint files located in the Resources/config/validation sub-directory of a bundle are loaded.
    Prior to 2.7, only Resources/config/validation.yml (or .xml) were loaded.

    More info at:

    • Github PR: [FrameworkBundle] "mappings" for validation #13878
    • Symfony doc: The Basics of Validation
    0 讨论(0)
提交回复
热议问题