How to split validation yaml files in Symfony 2.5?

后端 未结 3 487
栀梦
栀梦 2020-12-10 07:03

This code worked for Symfony 2.4.5 but it is not working for Symfony 2.5:

This is my HRAPI Extension class that was working in Symfony 2.4.5 but not in Symfony 2.5

相关标签:
3条回答
  • 2020-12-10 07:07

    Symfony 2.5 changed the way validation files were loaded. Here is how to do it now (using the Finder component to dynamically load):

    Create a compiler pass:

    namespace MyBundle\DependencyInjection\Compiler;
    
    use Symfony\Component\Finder\Finder;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\Config\Resource\DirectoryResource;
    
    class ValidatorPass implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            $validatorBuilder = $container->getDefinition('validator.builder');
            $validatorFiles = array();
            $finder = new Finder();
    
            foreach ($finder->files()->in(__DIR__ . '/../../Resources/config/validation') as $file) {
                $validatorFiles[] = $file->getRealPath();
            }
    
            $validatorBuilder->addMethodCall('addYamlMappings', array($validatorFiles));
    
            // add resources to the container to refresh cache after updating a file
            $container->addResource(new DirectoryResource(__DIR__ . '/../../Resources/config/validation'));
        }
    }
    

    Then, add this compiler pass in your Bundle:

    namespace MyBundle;
    
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use MyBundle\DependencyInjection\Compiler\ValidatorPass;
    
    class MyBundle extends Bundle
    {
        // . . . .
    
        public function build(ContainerBuilder $container)
        {
            parent::build($container);
    
            $container->addCompilerPass(new ValidatorPass());
        }
    
        // . . . .      
    }
    
    0 讨论(0)
  • 2020-12-10 07:15

    You should use ValidatorBuilder#addYamlMappings/ValidatorBuilder#addYamlMapping:

    $builderDefinition = $container->getDefinition('validator.builder');
    $builderDefinition->addMethodCall('addYamlMappings', array(
        array(
            __DIR__.'/../Resources/config/validation/site_user.yml';
            __DIR__.'/../Resources/config/validation/human_resource.yml';
            __DIR__.'/../Resources/config/validation/tag.yml';
            __DIR__.'/../Resources/config/validation/company.yml';
            __DIR__.'/../Resources/config/validation/programming_language.yml';
            __DIR__.'/../Resources/config/validation/technology.yml';
            __DIR__.'/../Resources/config/validation/language.yml';
            __DIR__.'/../Resources/config/validation/management.yml';
            __DIR__.'/../Resources/config/validation/calendar_entry.yml'
        )
    ));
    
    0 讨论(0)
  • 2020-12-10 07:31

    This works out-of-the box with Symfony 2.7.

    https://github.com/symfony/symfony/pull/13855

    0 讨论(0)
提交回复
热议问题