1
我想用Ajax方法提交表單,但是我沒有收到任何錯誤消息,表單也無效。如何在ajax表單無效時在視圖中顯示錶單錯誤?
ajax請求動作調用控制器「updateRoleAction」是好的,我用selrialize在ajax請求發送數據後。 Whene我通過警報顯示所有數據發送。我不明白爲什麼$editForm->isValid()
粘假。 AdminController.php的
public function updateRoleAction(Request $request, $id)
{
if ($this->container->get('request')->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('BISSAPUserBundle:Role')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Topic entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
$entities_role = $em->getRepository('BISSAPUserBundle:Role')->findAll();
return $this->render('BISSAPAdminForumBundle:Admin:role.html.twig', array(
'entities_role' => $entities_role, 'entity' => $entity,
));
}
return $this->render('BISSAPAdminForumBundle:Role:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'error_output' => "No post"
));
}
}
阿賈克斯jQuery的上
部分提出:
$(function(){
$(document).on('submit', "#form_edit_A", function(e) {
e.preventDefault();
var id = $("#bissap_userbundle_role_submit").data('id');
var scheme = "http://localhost";
var route = scheme.concat("/bodykoncept/web/app_dev.php/admin/admin/role/").concat(id).concat("/update");
var el = $('#td_role');
var $this = $(this);
alert($this.serialize());
$.ajax({type: 'POST', dataType: 'html', data: $this.serialize(), url: route, success: function(response){
el.html(response);
}, error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.status);
console.log(textStatus);
console.log(errorThrown);
}});
});
});
edit.html.twig:
{% block body -%}
<form name="bissap_userbundle_role" method="post" action="/bodykoncept/web/app_dev.php/admin/admin/role/" id="form_edit_A" role="form" class="tr selected">
<div class="td col-md-1">
{{ form_widget(edit_form._token) }}
{{ edit_form.vars.value.id }}
</div>
<div class="td col-md-3">
{{ form_widget(edit_form.role) }}
</div>
<div class="td col-md-3">
{{ form_widget(edit_form.description) }}
</div>
<div class="td col-md-3">
{{ form_widget(edit_form.groupRole) }}
</div>
<div class="td col-md-3">
<button type="submit" form="form_edit_A" id="bissap_userbundle_role_submit" name="bissap_userbundle_role[submit]" class="update_class btn" data-id="2">Update</button>
</div>
</form>
<div>{{ form_errors(edit_form) }}</div>
<div>{{ error_output}}</div>
{% endblock %}
這裏我嘗試從窗體顯示錯誤消息{{ form_errors(edit_form) }}
,但仍爲空。