How to use related select boxes in Symfony ?
Let's say, I have a select list containing compagnies and another containing employees of the selected company. How do I define those in Symfony? I have already created all the Javascript related code but when submitting my form and having errors on some fields, the all "sub" select fields are reset to null.
Any ideas?
Thanks,
EDIT : As the question seems to be misunderstood, I'll add some precisions :
Description :
- I have an entity Company containing a list of Employees using a @OneToMany relation.
- When I select a company in a select/dropdown list, the second dropdown list containing the employees is updated via jQuery. That part is done, works perfectly
- When submitting the form without errors, the entity form solution works fine.
- When submitting the form containing errors, the second dropdown list is containing all possible values. They are not filtered on the selected company.
Tried solutions :
- My first idea was to use the form entity type, thinking the component could be binded somehow on another field. ie. update list of employees based on the value of the selected company.
Not working, there is no way out of the box to do this. Even non out of the box solutions...
- Then I thought about manually passing the selected company as parameter to the query builder of the second dropdown list.
But when the form is created, the values are empty. The values are only set on bindRequest.
- Thought about using the choice type. Delegating all filter functionnality to the UI via Javascript. Meaning when the page loads, an empty list appears and is populated by Javascript based on the selected company.
This actually works, but I think there is no other word than really really ugly programming here.
PS :
Question has been asked here, on the Symfony2 mailing-list, on Twitter and the officiel Symfony 2 forum. I have of course searched each of those several times before posting my questions.
Concerning what you already tried, I think you should retry your first/second ideas:
You can populate the employees select box using the
entity
type. All you have to do is to define the good options:The example here filters the employees list based on the companyId form's option. You can modify this behavior by filtering directly on the company present in the form's data.
You still have to implement the
getEmployee()
andsetEmployee()
methods in yourEntity\Foo
class.No. Values are set when you create you form using form factory (third argument), OR when you call
$form->setData($foo);
. Data is modified when youbind
new inputs to the form.There could be a problem with this approach: it's possible that the employee id bound to the form is not available in the form choice list, because you changed the company (and thus the employees).
You can create action, that will be return json array with employees of company. When the list of companies changed you must request this action by ajax and create select list of employees.
Form controller action:
Conroller, that return employees by company in json:
Form template:
For more detail example you should describe your problem in more detail.
I had the same problem. You must use form events. My code example with country, region, city relations.