symfony - How to set the default value of a form Combobox field as the value in the database in Symfony2? -


i know how set default value of form combobox field value in database in symfony2. explanation below:

this code of entity dealing with:

    <?php  namespace ikproj\homebundle\entity;  use doctrine\orm\mapping orm;  /**  * user  *  * @orm\table(name="user")  * @orm\entity(repositoryclass="ikproj\homebundle\entity\userrepository")  */ class user {     /**      * @var integer      *      * @orm\column(name="id_user", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="name", type="string", length=255)      */     private $name;      /**      * @var string      *      * @orm\column(name="username", type="string", length=255)      */     private $email;      /**      * @var string      *      * @orm\column(name="pseudo", type="string", length=255)      */     private $pseudo;      /**      * @var string      *      * @orm\column(name="password", type="string", length=255)      */     private $passwd;      /**      * @var string      *      * @orm\column(name="sexeuser", type="string", length=255)      */     private $sexeuser;      /**      * @var \datetime      *      * @orm\column(name="dateanniv", type="date")      */     private $dateanniv;       /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * set name      *      * @param string $name      * @return user      */     public function setname($name)     {         $this->name = $name;          return $this;     }      /**      * name      *      * @return string       */     public function getname()     {         return $this->name;     }      /**      * set email      *      * @param string $email      * @return user      */     public function setemail($email)     {         $this->email = $email;          return $this;     }      /**      * email      *      * @return string       */     public function getemail()     {         return $this->email;     }      /**      * set pseudo      *      * @param string $pseudo      * @return user      */     public function setpseudo($pseudo)     {         $this->pseudo = $pseudo;          return $this;     }      /**      * pseudo      *      * @return string       */     public function getpseudo()     {         return $this->pseudo;     }      /**      * set passwd      *      * @param string $passwd      * @return user      */     public function setpasswd($passwd)     {         $this->passwd = $passwd;          return $this;     }      /**      * passwd      *      * @return string       */     public function getpasswd()     {         return $this->passwd;     }      /**      * set sexeuser      *      * @param string $sexeuser      * @return user      */     public function setsexeuser($sexeuser)     {         $this->sexeuser = $sexeuser;          return $this;     }      /**      * sexeuser      *      * @return string       */     public function getsexeuser()     {         return $this->sexeuser;     }      /**      * set dateanniv      *      * @param \datetime $dateanniv      * @return user      */     public function setdateanniv($dateanniv)     {         $this->dateanniv = $dateanniv;          return $this;     }      /**      * dateanniv      *      * @return \datetime       */     public function getdateanniv()     {         return $this->dateanniv;     } } 

this code of form belonged entity above:

    <?php  namespace ikproj\homebundle\form;  use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface;  class userprofilechangetype extends abstracttype {      /**      * @param formbuilderinterface $builder      * @param array $options      */     public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('name','text')                 ->add('sexeuser', 'choice', array('choices' => array('homme' => 'homme', 'femme' => 'femme')))                 ->add('dateanniv','date', array('input' => 'datetime','format' => 'yyyy-mm-dd','years' => range(1900, date("y"))));     }      /**      * @param optionsresolverinterface $resolver      */     public function setdefaultoptions(optionsresolverinterface $resolver)     {         $resolver->setdefaults(array(             'data_class' => 'ikproj\homebundle\entity\user'         ));     }      /**      * @return string      */     public function getname()     {         return 'ikproj_homebundle_user';     } } 

this code of view html\twig:

 <html>     <head>         <link rel="stylesheet" type="text/css" href="{{asset('bundles/ikprojhome/css2/css10.css')}}"/>         <script src='{{asset('bundles/ikprojhome/lib/jquery.min.js')}}'></script>     </head>     <body>         <center>             <div id="container">                 <div id="header">                 </div>                 <div id="content">                     <table width="100%" height="100%" align="center">                         <tr>                             <td>                                    <form action="{{path('ikproj_home_profilechange',{id:id})}}" method="post" {{ form_enctype(form) }} onsubmit="javascript:parent.jquery.fancybox.close();">                                     {{ form_errors(form) }}                                     <table align="center">                                         <tr>                                             <td class="separation">                                                 <label>nom:</label>                                             </td>                                             <td>                                                 {{ form_widget(form.name) }}                                             </td>                                         </tr>                                         <tr height="20px"></tr>                                         <tr>                                             <td class="separation">                                                 <label>sexe:</label>                                             </td>                                             <td>                                                 {{ form_widget(form.sexeuser) }}                                             </td>                                         </tr>                                         <tr height="20px"></tr>                                         <tr>                                             <td class="separation">                                                 <label>date de naissance:</label>                                             </td>                                             <td>                                                 {{ form_widget(form.dateanniv) }}                                             </td>                                         </tr>                                         <tr>                                             <td colspan="2" align="center" id="button" valign="bottom">                                                 <input class="button" type="submit" value="" id="update"/>                                             </td>                                         </tr>                                     </table>                                     {{form_widget(form._token)}}                                 </form>                             </td>                         </tr>                     </table>                  </div>             </div>         </center>     </body> </html> 

actually, value of field "sexeuser" in database (which relevant variable $sexeuser in code of entity above) "femme" whereas when page of view html\twig displayed default value displayed in combobox field "homme". so, question is: how can set default value of combobox field in database? (so, default value "femme" not "homme").

edit:

this relevant code in controller:

public function profilechangeaction($id, request $request) {     $em = $this->getdoctrine()->getmanager();     $profile1 = $em->getrepository('ikprojhomebundle:user')->find($id);     $form = $this->createform(new userprofilechangetype(), $profile1);     $profile2 = $form->getdata();     $form->handlerequest($request);     if ($request->ismethod('post')) {         if ($form->isvalid()) {             $em->persist($profile2);             $em->flush();             return $this->render('ikprojhomebundle:configuration:profilechange.html.twig');         }     } else {         return $this->render('ikprojhomebundle:configuration:profilechange.html.twig', array(                     'id' => $id,                     'form' => $form->createview()));     } } 

finally, resolved it, changed following line in form class:

->add('sexeuser', 'choice', array('choices' => array('homme' => 'homme', 'femme' => 'femme'))) 

to one:

->add('sexeuser', 'choice', array('choices' => array('homme' => 'homme', 'femme' => 'femme'),'empty_value' => null)) 

actually, can see above, added part of code: 'empty_value' => null .


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -