Shepherd
Shepherd

Reputation: 293

Symfony 2 problems with validating form

I want to validate my form. I created validation.yml file in the config folder, my bundle is registered and the file is loaded in DependencyInjection. I get the following error:

There is no extension able to load the configuration for "Developer\Forum\ForumBundle\Entity\Registration" (in /var/www/html/forum/src/Developer/Forum/ForumBundle/DependencyInjection/../Resources/config/validation.yml). Looked for namespace "Developer\Forum\ForumBundle\Entity\Registration", found none

My validation.yml:

Developer\Forum\ForumBundle\Entity\Registration:
    properties:
        name:
            - NotBlank: ~
        surname:
            - NotBlank: ~

DependencyInjection:

namespace Developer\Forum\ForumBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Developer\Forum\ForumBundle\Entity\Registration;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class DeveloperForumForumExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
        $loader->load('validation.yml');
    }
}

The form itself is working fine, data is saved into the database, but I need some validation. What is missing?

Upvotes: 2

Views: 457

Answers (1)

Aitch
Aitch

Reputation: 1697

/app/Resources/config/validation.yml is loaded automatically. Don't load it for your configuration, this is wrong.

Add this to your configuration instead, to enable validation and don't let PHP parse annotations.

framework:
    validation:
        enabled: true
        enable_annotations: false

If you want to add userdefined ymls for validation, you can append it to the parameter validator.mapping.loader.yaml_files_loader.mapping_files, (Symfony2 how to load validation.yml)

Upvotes: 2

Related Questions