Reputation: 29
I'm creating a form in Symfony2. The form has a field which is mapped to an entity array like this:
$builder
->add('card', 'entity', array(
'class' => 'AppBundle:Card',
'property' => 'description',
'choices' => $choices
));
I load the $choices array in my Controller. If I create the Form using the FormBuilder in the Controller I can use easily set the choices option, but I'd like to keep it in its own class to avoid bloating the Controller. Is there a clean way to inject the $choices array when creating the form?
Upvotes: 1
Views: 712
Reputation: 29
I'm not sure this is the best way, and it does seem a bit over-engineered, but I ended up defining the form as a service like this:
my_custom_form_service:
class: AppBundle\Form\MyFormType
calls:
- [ setUser, ["@security.context"] ]
tags:
- { name: form.type, alias: my_form }
I needed the user object because I obtain the choices array from that entity.
Upvotes: 1