嗨我试图在symfony中使用comboBox,模型依赖于商品,但我无法添加事件监听器`
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$marques = $em->getRepository('EcommerceBundle:Marque')->findAll();
$tableau = array();
foreach($marques as $marq)
{
array_push($tableau, $marq->getId());
}
$em1 = $this->getDoctrine()->getManager();
$modeles = $em->getRepository('EcommerceBundle:Modeles')->findAll();
$tableau1 = array();
foreach( $modeles as $model)
{
array_push($modeles, $model->getId());
}
$form=$this->createFormBuilder()
->add('marques', ChoiceType::class, array(
'choices' => $tableau,
'choice_label' => function ($key) {
$em = $this->getDoctrine()->getManager()->getRepository('EcommerceBundle:Marque');
$marques = $em->findOneById($key);
return $marques->getNom();
},
'attr'=>array('class' =>'id_of_the_form_field','value'=>'5')
))
->add('Modeles', ChoiceType::class, array(
'attr'=>array('class' =>'modeles','value'=>'5')
))
->add('Chercher', SubmitType::class, array('label' => 'Valider'))
->getForm();
$marque = function(FormInterface $form, $marques) {
$repository = $this->getDoctrine()->getRepository(Modeles::class);
$modele = $repository->findByMarque($marques);
if($modele) {
$mode = array();
foreach ($modele as $mod) {
$mod[$mod->getNom()] = $mod->getNom();
}
}else{
$mode=null;
}
$form->add('modele','choice', array('attr' => array('class' => 'modeles'),
'choices' => $mode));
};
$form->get('marques')->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($marques) {
$marques($event->getForm()->getParent(),$event->getForm()->getData());
});
$form->handleRequest($request);
if ($form->isSubmitted()) {
var_dump($form->getData());
return $this->redirect($this->generateUrl('presentzation', array('id' => $marque)));
}
return $this->render('EcommerceBundle:Marques:tousMarques.html.twig', array(
'marques' => $marques,'form' => $form->createView()
));
}
I have error in
尝试调用类“Symfony \ Component \ Form \ Form”的名为“addEventListener”的未定义方法。
`