4

I'm making a website with symfony2, and i have some troubles with form validaiton. It seems like method "isValid()" on my form is never called :

heres is my form creation :

public function createAction(Request $request) { $pro = new Professionnel(); $created=false;

    $form = $this->createFormBuilder($pro)
            ->add("raison_sociale",null,array("label"=>"Raison sociale * : "))
            ->add("siret",null,array("label"=>"N° SIREN * : "))
            ->add("nom",null,array("label"=>"Nom *: "))
            ->add("prenom",null,array(
                "label"=>"Prénom *: "
                ))
            ->add("adresse",null,array(
                "required"=>false,
                "label"=>"Adresse : "
                ))
            ->add("code_postal","text",array(
                "required"=>false,
                "label"=> "Code Postal : "
                ))
            ->add("ville",null,array(
                "required"=>false,
                "label"=> "Ville : "
                ))
            ->add("tel",null,array("label"=>"Téléphone * : ","required"=>true))
            ->add("mobile",null,array(
                "required"=>false,
                "label"=>"Mobile : "
                ))
            ->add("fax",null,array(
                "required"=>false,
                "label"=>"Fax : "
                ))
            ->add("email","email",array("label"=>"Email * : "))
            ->add("username",null,array("label"=>"Login * : "))
            ->add("password","password",array("label"=>"Mot de passe * : "))
            ->add("newsletter",null,array("required"=>false,"label"=>"Je souhaite m'inscrire à la newsletter"))
            ->add("accept","checkbox",array("label"=>" "))
            ->getForm()
    ;

    if ($request->isMethod("POST"))
    {
        $form->bind($request);
        if ($form->isValid())
        {
            $em=$this->getDoctrine()->getEntityManager();
            $pro=$form->getData();
            $encoder = $this->get('security.encoder_factory')->getEncoder($pro);
            $pro->setPassword($encoder->encodePassword($pro->getPassword(),$pro->getSalt()));
            $em->persist($pro);
            $em->flush();
            $created=true;
            $this->get("session")->getFlashBag()->add("success", "Votre compte a bien été créé. Vous avez reçu un mail confirmant votre inscritpion.");
        }
    }

    return $this->render('OverscanProfessionnelBundle:Front:create.html.twig',array('form'=>$form->createView(),'created'=>$created));
}

and here is my validatio.yml :

webapp\ProfessionnelBundle\Entity\Professionnel:
    constraints:
        Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: 
            fields: email
            message: "L'email est déjà pris"

        Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
            fields: username
            message: "Ce login est déjà pris"
    properties:
        raison_sociale:
             - Valid: 
        siret:
            - Length:
                min : 9
                max : 9
                minMessage: "Le SIREN doit contenir 9 caractères"
                maxMessage: "Le SIREN doit contenir 9 caractères" 
            - Regex:
                pattern: "/\d/"
                match: true
                message: "Le SIREN ne doit pas contenir de lettre"
        tel:
            - Length:
                min : 10
                max : 10
                minMessage: "Le numéro de téléphone doit contenir 10 chiffres"
                maxMessage: "Le numéro de téléphone doit contenir 10 chiffres"

so when i post my form no validation are called but constraints are ! Can someone help me please ?

8
  • what do you mean by no validation are called but constraints are ? please explain that ... Commented Jul 17, 2013 at 11:52
  • in my validation i have constraint for unique user mail. if i try to add un new record with same email address, i hava this notification message: "Ce login est déjà pris" throws by this constraint : Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: fields: username message: "Ce login est déjà pris" Commented Jul 17, 2013 at 12:06
  • yes i can see that ... and what is not working? Commented Jul 17, 2013 at 12:11
  • if I put 6 characters in field siret for example, the record would be saved, but i have a properties on this field for save it only if it have 9 characters Commented Jul 17, 2013 at 12:16
  • Are the properties not validated if you manually use the validator aswell? $validator = $this->get('validator'); $errors = $validator->validate($entity); ... Where exactly did you save your validation.yml ? Commented Jul 17, 2013 at 12:29

2 Answers 2

3

Take a look at your "app/../config.yml"
You should see something like this :

framework:
 ...
   validation: { enable_annotations: true }

Set it to :

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

And then, your validation.yml should be loaded !

Sign up to request clarification or add additional context in comments.

2 Comments

This work for me :) I had same problem, now it's solved! Thanks.
I don't know why I did not had the "validation" entry under "framework", but adding this just saved my day. Thanks for pointing this
0

Try adding the data class to your form like this:

$this->createFormBuilder($entity, array(
        'data_class' => '\Vendor\YourBundle\Entity\MyEntity',
    )
)
// -> add() 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.