Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FabienSalles/050d1d8dcce0ea76ef8cf40abd71bdc7 to your computer and use it in GitHub Desktop.
Save FabienSalles/050d1d8dcce0ea76ef8cf40abd71bdc7 to your computer and use it in GitHub Desktop.
Overide symfony validation mapping
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());
}
// . . . .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment