18

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
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
intrepion
  • 38,099
  • 4
  • 24
  • 21

8 Answers8

28

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);
}
Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
  • Thanks so much! This worked splendidly! I even deleted that validation.yml file – intrepion Feb 23 '12 at 17:08
  • @m2mdas This doesn't seem to work in xml by replacing yaml by xml. Is there a different synthax? I get the error message: [] operator not supported for strings.. – Mick Aug 04 '12 at 14:24
  • @Patt for xml the current parameter would be `validator.mapping.loader.xml_files_loader.mapping_files`. – Mun Mun Das Aug 04 '12 at 20:59
  • Excellent. Your answer really exposes the flexibility of the Sf2 framework. Thank you. – JustinP Nov 21 '12 at 16:42
  • 3
    This parameter was removed in Symfony 2.5 - see my answer below for updated instructions. – Kevin Bond Jun 10 '14 at 14:53
7

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:

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
6

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

Community
  • 1
  • 1
Kevin Bond
  • 724
  • 6
  • 16
5

You can add to your 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>
3

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.

nass600
  • 599
  • 2
  • 11
  • 22
2

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
KLXN
  • 660
  • 5
  • 13
2

Since Symfony 3.3 all files present in YourBundle/Resources/config/validation/* are registered.


Since Symfony 3.4 all files present in %kernel.project_dir%/config/validator/* are also registered.

Fabien Salles
  • 1,101
  • 15
  • 24
1

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%)?

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62