微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony2发送表单ajax

我正在尝试通过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来创建查询和其他值来更新实体?
如果这些字段不通过验证,我该如何传递错误

解决方法

我建议把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;
}

原文地址:https://www.jb51.cc/html/224885.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐