gpt4 book ai didi

php - 当ajax表单无效时,如何在 View 中显示表单错误?

转载 作者:行者123 更新时间:2023-12-01 04:03:16 24 4
gpt4 key购买 nike

我尝试使用 Ajax 方法提交表单,但没有收到错误消息,并且表单无效。

在ajax请求之后,调用action Controller“updateRoleAction”就可以了,我在ajax请求中使用了selrialize来发送数据post。当我通过警报显示此信息时,所有数据均已发送。我不明白为什么 $editForm->isValid() 坚持 False。

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"
));
}

}

提交时使用 Ajax 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) }},但仍然为空。

最佳答案

这可能是因为错误附加到了相应的字段。

如果您希望将错误映射到顶级表单,您应该尝试在表单类型的 configureOptions() 方法中添加此行:

// src/AppBundle/Form/Type/EditForm.php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class EditFormType extends AbstractType
{
// ...

public function configureOptions(OptionsResolver $resolver)
{
// ...

$resolver->addDefault('error_bubbling', true);
}

// ...
}

要了解有关此选项的更多信息,请参阅 the official documentation .

关于php - 当ajax表单无效时,如何在 View 中显示表单错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com