inaliahgle inaliahgle - 1 year ago 89
PHP Question

Symfony2 Form : Preselected value is lost in editAction

It seems that I'm doing something wrong, but I don't know where.
I have a Choicelist

like the following :

$builder->add('motif', 'choice', array(
'label' => 'Motif',
'choice_list' => $motifs, //array of entities
'empty_value' => "Please select motif ..."


class Motif{

public $id;

public $code;

public function __toString(){
return $this->code;

The form works fine when I want to create an entity. But, in edit, I lost the initial selection and I got "Please select motif...".

Thanks in advance for your help.

Answer Source

I finally foud the cause of the problem. it's a Symfony Framwork issue : [Form] ObjectChoiceList should select initial field value based on the value property.

I Had to write a class that I named ValueBasedObjectChoiceList in order to overcome the problem.

I hope this could help people having the same issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download