19271

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

Question:

I would like to know how to set the default value of a form Combobox field as the value in the database in Symfony2. The explanation is as below:

This is the code of the entity I am dealing with:

<?php namespace Ikproj\HomeBundle\Entity; use Doctrine\ORM\Mapping as 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; /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get 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; } /** * Get dateanniv * * @return \DateTime */ public function getDateanniv() { return $this->dateanniv; } }

This is the code of the form belonged to the 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 is the code of the 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, the value of the field "sexeuser" in the database (which is relevant to the variable $sexeuser in the code of the entity above) is "Femme" whereas when the page of the view html\twig is displayed the default value displayed in the Combobox field is "Homme". So, my question is: how can I set the default value of the Combobox field as exactly what is in the database? (so, the default value will be "Femme" not "Homme").

<strong>Edit:</strong>

this is the relevant code in the 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())); } }

Answer1:

Finally, I resolved it, I just changed the following line in the form class:

->add('sexeuser', 'choice', array('choices' => array('Homme' => 'Homme', 'Femme' => 'Femme')))

to this one:

->add('sexeuser', 'choice', array('choices' => array('Homme' => 'Homme', 'Femme' => 'Femme'),'empty_value' => null))

Actually, as you can see above, I added this part of code: 'empty_value' => null .

Recommend