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
                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);
}
                        As of Symfony
2.7, XML and Yaml constraint files located in theResources/config/validationsub-directory of a bundle are loaded.
Prior to2.7, onlyResources/config/validation.yml(or .xml) were loaded.
More info at:
Symfony 2.5 broke the above solutions. See: https://stackoverflow.com/a/24210501/175753
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With