validation.yml文件被忽略

时间:2016-09-13 12:43:45

标签: php validation symfony symfony-forms

我是symfony的新手,我目前正在尝试了解Symfony3。 虽然通过注释验证工作正常,但我无法通过validation.yml文件激活验证。

在我的app / config / config.yml中,我改变了这一行:

framework:
    validation:      { enabled: true, enable_annotations: false }

我的src / AppBundle / Resources / config / validation.yml是:

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\TaskSetClass:
    properties:
        taskName:
            - NotBlank: ~
        dueDate:
            - NotBlank: ~
            - Type: \DateTime

我添加了一个扩展文件:

<?php
// src/AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Finder\Finder;
/* */
class AppExtension extends Extension
    {
    public function load(array $configs, ContainerBuilder $container)
        {
        $container=new ContainerBuilder();
        // Services
        $loader=new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../../app/config'));
        $loader->load('services.yml');
        // Validation
        // Commented : No "validator.mapping.loader.yaml_files_loader.mapping_files" parameter.
        //$yamlMappingFiles=$container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');
        $finder=new Finder();
        foreach($finder->files()->in(__DIR__.'/../../../app/Resources/config') as $file)
            {
            $filePath=$file->getRealPath();
            if(preg_match('@\.yml$@',$filePath)===1)
                {
                $yamlMappingFiles[]=$filePath;
                }
            }
        $container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $yamlMappingFiles);
        }
    }

在appExtension.php中的setParameter()之后转储容器显示命令已成功:

[parameterBag:protected] => Symfony\Component\DependencyInjection\ParameterBag\ParameterBag Object
    (
    [parameters:protected] => Array
        (
        [validator.mapping.loader.yaml_files_loader.mapping_files] => Array
            (
            [0] => /mnt/400Go/www/sy1/app/Resources/config/validation.yml
            )
        )
    [resolved:protected] => 
    )

在我的表单控制器中,如果列出约束:

[...]
if($form->isSubmitted())
    {
    if($form->isValid())
        {
        $metadata=$this->container
                 ->get('validator')
                 ->getMetadataFor('AppBundle\Entity\TaskSetClass');
        $propertiesMetadata=$metadata->properties;
        $constraints=array();
        foreach ($propertiesMetadata as $propertyMetadata)
            {
            $constraints[$propertyMetadata->name]=$property->constraints;
            }
        echo'<pre>Constraints : ';
        print_r($constraints);
        echo'</pre>';
        die();
        [...]

......数组为空。所以文件似乎被加载,但约束被忽略。

有没有人遇到过这个错误?它可能来自symfony 2和3之间的区别。

1 个答案:

答案 0 :(得分:0)

[求助]验证文件不在正确的目录中。它应该在AppBundle / Resources / config中,我把它放到app / Resouces / config中。现在工作正常。