对不起,但我的英语不好。
我正在Symfony 2.3.6中开发一个应用程序,我必须在DB(postgres)中保存用户的值。 当我的嵌入表单(pais + region)有2个提交按钮时,用户按下第二个按钮就会出现问题。系统抛出
Child "salvar_region" does not exist.
问题是如果系统抛出异常,如何检测第二个按钮是否被按下?
这是我的默认控制器
public function addLocalAction(Request $request)
{
$region = new Region();
$localidad = new Pais();
$localidad->setRegion($region);
$form = $this->createForm(new localidadType(), $localidad);
if($request->isMethod('POST')){
$form->handleRequest($request);
$form->getData();
if ($form->get('salvarPais')-> isClicked()){
$objeto = new Pais();
$objeto->setNombrePais($form->get('nombrePais')->getData());
$em = $this->getDoctrine()->getManager();
$em->persist($objeto);
$em->flush();}
if ($form->get('salvarregion')->isClicked()){
$objeto = new Region();
$objeto->setNombreRegion($form->get('nombreRegion')->getData());
$objeto->setNombreRegion($form->get('numeroRegion')->getData());
$em = $this->getDoctrine()->getManager();
$em->persist($objeto);
$em->flush();}
return $this->redirect($this->generateUrl('localidades'));
}
return $this->render('plataformaPlataformaBundle:Default:localidades.html.twig', array('form'=> $form->createView()));
}
我的html.twig
<form action="{{ path('localidades')}}" method="post" {{ form_enctype(form) }}>
<h3>Agregar País</h3>
<div class="pais">
{{ form_row(form.nombrePais) }}
{{ form_row(form.salvarPais) }}
</div>
<form action="{{ path('localidades')}}" method="post" {{ form_enctype(form) }}>
<h3>Agregar Region</h3>
<div class="region">
{{ form_start(form) }}
{{ form_errors(form)}}
{{ form_row(form.region.nombreRegion) }}
{{ form_row(form.region.numeroRegion) }}
{{ form_row(form.region.pais) }}
{{ form_row(form.region.salvarregion) }}
{{ form_end(form) }}
</div>
</form>
和我的2种表格
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nombreRegion', 'text', array('label' => 'Nombre de Región: '));
$builder->add('numeroRegion', 'integer', array('label' => 'Número de Región: '));
$builder->add('pais', 'entity', array('label' => 'País al que pertenece: ',
'class' => 'plataformaPlataformaBundle:Pais',
'property' => 'nombrePais'));
$builder->add('salvarregion', 'submit', array('label' => 'Guardar Región'));
$builder->getForm();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nombrePais', 'text');
$builder->add('salvarPais', 'submit', array('label' => 'Guardar País'));
$builder->add('region', new regionType());
$builder->getForm();
}
PD:非常感谢有人可以帮助我
PDx2:这是URL请求的示例:{“region”:{“nombreRegion”:“ewrew”,“numeroRegion”:“4”,“pais”:“3”,“salvarregion”:“”},“ _token “:” 853d5460b076e01fdf4cef4c59c33e836ab64af3" }
答案 0 :(得分:0)
您获得的错误与构建过程中给出按钮的名称不同。
无论如何,要判断是否点击了特定提交,您可以使用:
if($form->get('salvarregion')->isClicked()) {
}