I am creating my first app on symfony, and am currently working on a form in order to edit an pretty big entity. My issue is : i have some choiceType in order to choose from definied values... BUT when i enter in the form, the default value of these choiceType is set to the first one in the list, not the value of the entity...
Same goes for an EntityType...
There is my buildform :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('classePSI', NumberType::class, array(
'attr' => array(
'min' => 0,
'max' => 5
)
))
->add('codeSA', TextType::class)
->add('nom', TextType::class)
->add('host', TextType::class)
->add('cluster', TextType::class)
->add('besoin', ChoiceType::class, array(
'choices' => array(
'0 - Non concerné' => 0,
'1 - A remonter' => 1,
'2 - Composant commun' => 2
)
))
->add('estVirtuel', ChoiceType::class, array(
'choices' => array(
'Reel' => false,
'Virtuel' => true
)
))
->add('instancePartage', TextType::class, array('required' => false))
->add('perimetre', EntityType::class, array(
'class' => 'GGMOPSIBundle:Perimetre',
'choice_label' => 'libellePerimetre',
'multiple' => false,
'expanded' => false,
'query_builder' => function (PerimetreRepository $repository) {
return $repository->getVisibleQB();
}
))
->add('type', EntityType::class, array(
'class' => 'GGMOPSIBundle:SousType',
'choice_label' => 'nom',
'multiple' => false,
'expanded' => false
))->add('save', SubmitType::class, array("label" => "Editer composant"));;
}
My entity :
namespace G\GMOPSIBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Composant
*
* @ORM\Table(name="gmo_psi_composant")
* @ORM\Entity(repositoryClass="G\GMOPSIBundle\Repository\ComposantRepository")
*/
class Composant
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="classePSI", type="integer")
*/
private $classePSI;
/**
* @var string
*
* @ORM\Column(name="codeSA", type="string", length=20)
*/
private $codeSA;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="host", type="string", length=255)
*/
private $host;
/**
* @var string
*
* @ORM\Column(name="cluster", type="string", length=255)
*/
private $cluster;
/**
* @var int
*
* @ORM\Column(name="besoin", type="integer",nullable=true)
*/
private $besoin=null;
/**
* @var bool
*
* @ORM\Column(name="estVirtuel", type="boolean")
*/
private $estVirtuel;
/**
* @var string
*
* @ORM\Column(name="InstancePartage", type="string", length=255,nullable=true)
*/
private $instancePartage;
/**
* @ORM\ManyToOne(targetEntity="G\GMOPSIBundle\Entity\Perimetre")
* @ORM\JoinColumn(nullable=true)
*/
private $perimetre;
/**
* @ORM\ManyToOne(targetEntity="G\GMOPSIBundle\Entity\SousType")
* @ORM\JoinColumn(nullable=true)
*/
private $type;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set classePSI
*
* @param integer $classePSI
*
* @return Composant
*/
public function setClassePSI($classePSI)
{
$this->classePSI = $classePSI;
return $this;
}
/**
* Get classePSI
*
* @return integer
*/
public function getClassePSI()
{
return $this->classePSI;
}
/**
* Set codeSA
*
* @param string $codeSA
*
* @return Composant
*/
public function setCodeSA($codeSA)
{
$this->codeSA = $codeSA;
return $this;
}
/**
* Get codeSA
*
* @return string
*/
public function getCodeSA()
{
return $this->codeSA;
}
/**
* Set nom
*
* @param string $nom
*
* @return Composant
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set host
*
* @param string $host
*
* @return Composant
*/
public function setHost($host)
{
$this->host = $host;
return $this;
}
/**
* Get host
*
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* Set cluster
*
* @param string $cluster
*
* @return Composant
*/
public function setCluster($cluster)
{
$this->cluster = $cluster;
return $this;
}
/**
* Get cluster
*
* @return string
*/
public function getCluster()
{
return $this->cluster;
}
/**
* Set besoin
*
* @param integer $besoin
*
* @return Composant
*/
public function setBesoin($besoin)
{
$this->besoin = $besoin;
return $this;
}
/**
* Get besoin
*
* @return integer
*/
public function getBesoin()
{
return $this->besoin;
}
/**
* Set estVirtuel
*
* @param boolean $estVirtuel
*
* @return Composant
*/
public function setEstVirtuel($estVirtuel)
{
$this->estVirtuel = $estVirtuel;
return $this;
}
/**
* Get estVirtuel
*
* @return boolean
*/
public function getEstVirtuel()
{
return $this->estVirtuel;
}
/**
* Set instancePartage
*
* @param string $instancePartage
*
* @return Composant
*/
public function setInstancePartage($instancePartage)
{
$this->instancePartage = $instancePartage;
return $this;
}
/**
* Get instancePartage
*
* @return string
*/
public function getInstancePartage()
{
return $this->instancePartage;
}
/**
* @return mixed
*/
public function getPerimetre()
{
return $this->perimetre;
}
/**
* @param mixed $perimetre
*/
public function setPerimetre($perimetre)
{
$this->perimetre = $perimetre;
}
/**
* @return Type
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type)
{
$this->type = $type;
}
}
My editAction :
public function editAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$composant = $em->getRepository('GGMOPSIBundle:Composant')->find($id);
if (null === $composant) {
throw new NotFoundHttpException("Le composant d'id ".$id." n'existe pas.");
}
$form = $this->createForm(ComposantType::class, $composant);
$handle =$form->handleRequest($request);
if ($request->isMethod('POST') && $handle->isSubmitted() && $handle->isValid()) {
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Composant bien modifié.');
return $this->redirectToRoute('g_gmopsi_viewcomposants');
}
return $this->render('GGMOPSIBundle:Composant:edit.html.twig', array(
'comp' => $composant,
'form' => $form->createView(),
));
}
Every textype is correctly pre-entered with values from entity so i don't know what to do...
Thanks in advance