1

To validate my class DeclarationForm :

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

I use a validation.yml in the bundle :

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

I checked the app/config.yml

framework:
   validation:      { enabled: true }

It is like validation.yml does not exist. form->isValid() returns 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()) {
    ....
}

I modified the code not to use isValid :

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

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

It works !!

So the problem is (isValid). I dont understand why it does not work

mlwacosmos
  • 4,391
  • 16
  • 66
  • 114
  • Did you `cache:clear`? – Jovan Perovic Oct 02 '14 at 12:45
  • So isValid is always true when you post? Not doing anything strange in DeclarationType? – Cerad Oct 02 '14 at 12:45
  • Possible duplicate: http://stackoverflow.com/questions/12367791/symfony2-how-to-load-validation-yml See the answer of Alberto Gaona. He emphasized the importance of disabling the annotations... – Jovan Perovic Oct 02 '14 at 13:10
  • yes isValid is always true – mlwacosmos Oct 02 '14 at 13:12
  • normally I dont have to load validation.yml.. but even though I try to load it I have an exception saying that there is no extension that could load xxx\MyBundle\Form\DeclarationForm: – mlwacosmos Oct 02 '14 at 13:13
  • Is that a perfect copy of your `validation.yml`? The second line (`properties:`) should be indented. If it's not, then that may be causing the config not to be read properly. – frumious Oct 02 '14 at 14:43
  • right... I modified the post – mlwacosmos Oct 02 '14 at 14:47
  • obviously the yml file is not the problem because by changing the code and not using isValid, it works – mlwacosmos Oct 02 '14 at 14:48
  • I just checked how the isValid() method does validation, and in fact it's implemented as an Event Listener listening for the POST_SUBMIT event. This Listener is wired up by ValidatorExtension, I haven't been able to find where it "chooses" to do that. So the only thing I can think of is that for some reason your ValidationListener isn't being set up, but I'm not sure why that would happen. Have you got your own FormFactory or anything like that? – frumious Oct 05 '14 at 14:42
  • I don't have my own FormFactory – mlwacosmos Oct 06 '14 at 08:46

1 Answers1

0

Are you sure it runs the validation file. In order to use the yml validation file, you need to map them so that symfony knows which file to run for the validation.

You can do that in the DependencyInjection/{$bundleName}Extension.php file into your bundle like so :

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

I've been strungling with the same issue for some and this solved it.

Hope this helped.

Fougere
  • 187
  • 3
  • 8
  • Calling the validator by hand, I am 200% sure it calls the yaml. So the yaml is not the problem. I am sure because I translated the error messages in the yaml. So there is no doubt. I see 2 possibilities : 1. isValid does not call the validator service or 2. when using isValid on the form it does not find the class to validate – mlwacosmos Oct 03 '14 at 08:34