inaliahgle inaliahgle - 8 days ago 7
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

motif
like the following :

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


The
Motif
Entity:

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

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.

Comments