Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

yaml

symfony

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\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

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
like image 360
intrepion Avatar asked Feb 22 '12 21:02

intrepion


3 Answers

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);
}
like image 103
Mun Mun Das Avatar answered Sep 29 '22 08:09

Mun Mun Das


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
like image 29
felipsmartins Avatar answered Sep 29 '22 07:09

felipsmartins


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

like image 30
Kevin Bond Avatar answered Sep 29 '22 06:09

Kevin Bond