Symfony2 发送表单 ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15053529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 send form ajax
提问by Lughino
I'm trying to submit a form via ajax to update a field of an entity, but do not know how to retrieve the data from the controller:
我正在尝试通过 ajax 提交表单以更新实体的字段,但不知道如何从控制器检索数据:
<form class="ajax" action="{{ path('ajax_setSocial') }}" method="post" {{ form_enctype(form) }}>
<div class="editor">
{{ form_errors(form) }}
<div class="editLabel pls">{{ form_label(form.ragSocial) }}</div>
<div class="editField">
<div class="ptm">
{{ form_widget(form.ragSocial) }} {{ form_errors(form.ragSocial) }}
</div>
{{ form_rest(form) }}
<div class="mtm">
<button class="btn btn-primary disabled save" type="submit">Save</button>
<button class="btn ann">Close</button>
</div>
</div>
</div>
var url = Routing.generate('ajax_setSociale');
var Data = $('form.ajax').serialize();
$.post(url,
Data
, function(results){
if(results.success == true) {
$(this).parents('ajaxContent').remove();
$(this).parents('.openPanel').removeClass('openPanel');
} else {
alert('False'); //test
}
});
controller (ajax_setSocial route)
控制器(ajax_setSocial 路由)
public function setSocialeAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
// $id = $request->get('form'); ???
$entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Anagrafic entity.');
}
$form = $this->createFormBuilder($entity)
->add('ragSocial', 'text', array('label' => 'Social'))
->add('id', 'hidden')
->getForm();
$form->bind($request);
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
$output = array();
$response = new Response();
$output[] = array('success' => true);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
}
As recovery values ??and then pass the id to create the query and the other values ??to update the entity? And if the fields do not pass validation, how do I pass the error?
作为恢复值?然后传递id来创建查询和其他值??更新实体?如果字段未通过验证,我该如何通过错误?
采纳答案by ihsan
I suggest to pass the id to the controller.
我建议将 id 传递给控制器。
html:
html:
<form class="ajax" action="{{ path('ajax_setSocial', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}>
var url = "{{ path('ajax_setSocial', { 'id': entity.id }) }}";
The controller annotation, parameter, and return value, to get the id:
控制器注解、参数和返回值,获取id:
/**
*
* @Route("/{id}", name="ajax_setSocial")
* @Method("POST")
*/
public function setSocialeAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
return array(
'entity' => $entity
);
}
Passing error back to html is like this:
将错误传回 html 是这样的:
// dummy line to force error:
// $form->get('ragSocial')->addError(new FormError("an error message"));
if ($form->isValid()) {
...
} else {
$errors = $form->get('ragSocial')->getErrors(); // return array of errors
$output[] = array('error' => $errors[0]->getMessage()); // the first error message
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
}
回答by Lighthart
I think you want this:
我想你想要这个:
However, this one may also be useful:
但是,这个也可能有用:
Many-to-Many Ajax Forms (Symfony2 Forms)(Answer 3)
多对多 Ajax 表单(Symfony2 表单)(答案 3)

