大佬教程收集整理的这篇文章主要介绍了Symfony2发送表单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 } });
控制器(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; }
作为恢复值,然后传递id来创建查询和其他值来更新实体?
如果这些字段不通过验证,我该如何传递错误?
HTML:
<form class="ajax" action="{{ path('ajax_setSocial',{ 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}> var url = "{{ path('ajax_setSocial',{ 'id': entity.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 ); }
将错误传回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; }
以上是大佬教程为你收集整理的Symfony2发送表单ajax全部内容,希望文章能够帮你解决Symfony2发送表单ajax所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。