TheMouk
TheMouk

Reputation: 41

symfony validation form doesn't call

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 ?

Upvotes: 4

Views: 2021

Answers (2)

peltho
peltho

Reputation: 116

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 !

Upvotes: 3

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52463

Try adding the data class to your form like this:

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

Upvotes: 0

Related Questions