I want to create a dependend select box: if the first selectbox is selected, the second selectbox should be filled.
My first selectbox isn't mapped to the model. I set the value manually in my controller with $form->get('emailTemplateBlockType')->setData($emailTemplateBlockType)
.. How can I use this data in my form event to create my second selectbox?
$builder
->add('emailTemplateBlockType', 'entity', array(
'class' => 'MbDbMVibesBundle:EmailTemplateBlockType',
'property' => 'name',
'mapped' => false,
'empty_value' => 'Choose a block type',
'attr' => array(
'class' => 'emailTemplateBlockTypeSelect',
)
))
->add('save', 'submit');
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
echo "name: ".$form->get('emailTemplateBlockType')->getData()->getName();
die;
});
I have an onChange event with jQuery that posts the choice of the first selectbox to my frontcontroller again. The front controller then creates the form again, but this time with the value of the first selectbox added. But since it's not a submit, I think using the POST_SUBMIT event will not work either.
Snippet from my controller:
$form = $this->createForm(new EmailTemplateSiteEmailTemplateBlockType(), $entity, array(
'action' => $this->generateUrl('_email_tpl_site_block_edit', array(
'emailTemplateSiteId' => $emailTemplateSiteId,
'emailTemplateSiteBlockId' => $emailTemplateSiteBlockId,
))
));
if ($request->request->get('blockTypeId')) {
$this->get('logger')->debug('setting block type');
$emailTemplateBlockType = $em->getRepository('MbDbMVibesBundle:EmailTemplateBlockType')
->find($request->request->get('blockTypeId'));
if ($emailTemplateBlockType)
$form->get('emailTemplateBlockType')->setData($emailTemplateBlockType);
else
throw new $this->createNotFoundException('Cannot find blocktype with id '.$request->request->get('blockTypeId'));
}
$form->handleRequest($request);
POST_SUBMIT
instead?