Symfony 2验证表单的问题

时间:2015-02-08 14:22:34

标签: php validation symfony

我想验证我的表单。我在config文件夹中创建了validation.yml文件,我的bundle已注册,文件在DependencyInjection中加载。我收到以下错误:

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

我的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');
    }
}

表单本身工作正常,数据保存到数据库中,但我需要一些验证。缺少什么?

1 个答案:

答案 0 :(得分:2)

/app/Resources/config/validation.yml自动加载。不要为你的配置加载它,这是错误的。

将此添加到您的配置中,以启用验证并且不要让PHP解析注释。

framework:
    validation:
        enabled: true
        enable_annotations: false

如果要添加用户定义的ymls进行验证,可以将其附加到参数validator.mapping.loader.yaml_files_loader.mapping_files,(Symfony2 how to load validation.yml