Symfony 2:validation.yml无效

时间:2014-10-02 11:33:54

标签: forms validation symfony

验证我的类DeclarationForm:

class DeclarationForm {
  private $identifiant;
  private $qualiteInterlocuteur;
  private $nomCible;
  private $reference;
  private $description;
  private $traitement;
  private $commentaire;
}

我在捆绑包中使用validation.yml:

xxx\MyBundle\Form\DeclarationForm:
  properties:
    nomCible:
        - NotBlank: { message: 'not good' }
    description:
        - NotBlank: { message: 'not good' } 

我检查了app / config.yml

framework:
   validation:      { enabled: true }

就像validation.yml不存在一样。 form-> isValid()返回true

$declaration_form = new DeclarationForm();
$form = $this->createForm(new DeclarationType($this->get('translator'), array(
            'phase' => $phase, 
            'params_qualities' => $params
        )), $declaration_form);
$form->handleRequest($request);
if($form->isValid()) {
    ....
}

我修改了代码,不使用isValid:

    $validator = $this->get('validator');
    $liste_erreurs = $validator->validate($declaration_form);

    if(count($liste_erreurs) === 0)

它有效!!

所以问题是(isValid)。我不明白为什么它不起作用

1 个答案:

答案 0 :(得分:0)

您确定它运行验证文件吗?为了使用yml验证文件,您需要映射它们,以便symfony知道要运行哪个文件进行验证。

您可以将DependencyInjection/{$bundleName}Extension.php文件中的内容添加到您的软件包中,如下所示:

class DemoExtension extends Extension
{

    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');

        // Validation
        $yamlMappingFiles = $container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');

        $yamlMappingFiles[] = __DIR__.'/../Resources/config/validation/validationFile.yml';

        $container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $yamlMappingFiles);
    }
}

对于某些人,我一直在纠缠同样的问题,这解决了它。

希望这会有所帮助。