twig ajax异常,Symfony2 ajax twig形成错误(Symfony2 ajax twig form errors)

博客讨论了如何在Symfony2中优雅地显示通过AJAX提交表单时遇到的错误。当前的方法返回了一个错误字符串,显示了所有字段的错误,包括没有错误的字段。提问者寻求一种方式来格式化这些错误,只显示有错误的字段,并使错误消息更易读。回答建议使用$form->getErrors()代替$form->getErrorsAsString(),以便获取FormError对象并创建自定义错误消息。
摘要由CSDN通过智能技术生成

Symfony2 ajax twig形成错误(Symfony2 ajax twig form errors)

有一个问题,当他们从ajax响应回来时,如何在Symfony2中正确输出表单提交错误。

我通过ajax发布表单,如果表单没有正确填写,它将使用以下代码发回错误的响应...

$errors = $form->getErrorsAsString();

$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errors);

这将创建一个错误数组和其他变量,如下所示:

{"responseCode":200,"responseVal":"Error","errorReport":"ERROR: Name cannot be blank.\nERROR: Address cannot be blank.\nERROR: City cannot be blank.\nERROR: State cannot be blank.\nERROR: Zip cannot be blank.\nERROR: Phone cannot be blank.\nERROR: Email cannot be blank.\nname:\n No errors\naddress:\n No errors\ncity:\n No errors\nstate:\n No errors\nzip:\n No errors\nemail:\n No errors\nfax:\n No errors\nphone:\n No errors\n"}

然后我使用jQuery将错误写入div,如下所示:

$("#errorReport").html(data.errorReport);

这给了我一个div,其中包含以下内容:

ERROR: Name cannot be blank. ERROR: Address cannot be blank. ERROR: City cannot be blank. ERROR: State cannot be blank. ERROR: Zip cannot be blank. ERROR: Phone cannot be blank. ERROR: Email cannot be blank. name: No errors address: No errors city: No errors state: No errors zip: No errors email: No errors fax: No errors phone: No errors

这看起来很俗气。 无论如何在Twig或Symfony中我可以格式化这些错误,以便它们在传递回树枝模板时看起来很漂亮吗? 我希望它看起来像这样,但我不知道它是如何完成的:

Name cannot be blank.

Address cannot be blank.

City cannot be blank.

State cannot be blank.

Zip cannot be blank.

Phone cannot be blank.

Email cannot be blank.

(any of the "No errors" would not be shown)

非常感谢你的帮助!!!

Got a question about how to properly output form submission errors in Symfony2 when they come back from an ajax response.

I am posting a form via ajax, if the form is not filled in correctly, it will send back a reponse with the errors using the following code...

$errors = $form->getErrorsAsString();

$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errors);

This will create an array of errors and other variables like so:

{"responseCode":200,"responseVal":"Error","errorReport":"ERROR: Name cannot be blank.\nERROR: Address cannot be blank.\nERROR: City cannot be blank.\nERROR: State cannot be blank.\nERROR: Zip cannot be blank.\nERROR: Phone cannot be blank.\nERROR: Email cannot be blank.\nname:\n No errors\naddress:\n No errors\ncity:\n No errors\nstate:\n No errors\nzip:\n No errors\nemail:\n No errors\nfax:\n No errors\nphone:\n No errors\n"}

I am then using jQuery to write the errors to a div, like so:

$("#errorReport").html(data.errorReport);

This gives me a div with the following content:

ERROR: Name cannot be blank. ERROR: Address cannot be blank. ERROR: City cannot be blank. ERROR: State cannot be blank. ERROR: Zip cannot be blank. ERROR: Phone cannot be blank. ERROR: Email cannot be blank. name: No errors address: No errors city: No errors state: No errors zip: No errors email: No errors fax: No errors phone: No errors

This looks really tacky. Is there anyway in Twig or Symfony that I can format these errors so that it looks presentable when they are passed back to the twig template? I'd like it to look like this, but I just don't know how its done:

Name cannot be blank.

Address cannot be blank.

City cannot be blank.

State cannot be blank.

Zip cannot be blank.

Phone cannot be blank.

Email cannot be blank.

(any of the "No errors" would not be shown)

Thanks so much for your help!!!

原文:https://stackoverflow.com/questions/15581286

更新时间:2020-09-07 16:09

最满意答案

你应该使用$form->getErrors()方法而不是$form->getErrorsAsString(); getErrors函数返回FormError对象,可用于创建错误消息

所以代码看起来像这样

$errors = $form->getErrors();

$errorCollection = array();

foreach($errors as $error){

$errorCollection[] = $error->getMessageTemplate()

}

$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errorCollection);

You should use the $form->getErrors() method instead of $form->getErrorsAsString(); getErrors function Returns FormError Object which can be used to create your error messages

So the code would look something like this

$errors = $form->getErrors();

$errorCollection = array();

foreach($errors as $error){

$errorCollection[] = $error->getMessageTemplate()

}

$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errorCollection);

相关问答

由于在2.3中删除了field_*因此已批准的解决方案将不再有效。 我找到的一个hacky解决方案是 {% block form_errors %}

{% if errors|length > 0 %}

{% if form.parent is empty %}

{% for error in errors %}

{{ err

...

在发出ajax请求后,您的表单似乎正常submit ,因为即使您已经发出请求,也会触发正常的默认submit事件。 你试过添加: e.preventDefault():

在: $("#frm_newsletter").submit(function(e) {

e.preventDefault();

// rest of your code

}

? (注意函数参数中的e ) It seems your form get submitted normally after the aj

...

你应该使用$form->getErrors()方法而不是$form->getErrorsAsString(); getErrors函数返回FormError对象,可用于创建错误消息 所以代码看起来像这样 $errors = $form->getErrors();

$errorCollection = array();

foreach($errors as $error){

$errorCollection[] = $error->getMessageTemplate()

}

$ret

...

你是如何生成symfony 2表单的? 据我所知,生成tinymce是一件简单的事情,你只需为你希望变成一个小mce的表单输入设置一个id / class,然后你设置你的javascript来寻找那个id / class和改造它。 要为symfony中的表单字段指定特殊ID,请执行以下操作: {{form_widget(form.text,{'id':'wysiwig_full'})}}

这将呈现一个名为text的字段,其id为wysiwig_full。 用任何字段名称替换文本。 I was a

...

Richard Miller撰写了一篇很好的文章,展示了实现AJAX使用的部分模板的好方法。 该文章称为“ Symfony2:Ajax和整页模板 ”。 后来他发表了第二篇 - 更详细的文章,另外还有我的建议,称为“ 更多关于AJAX模板 ”。 这两篇文章(尤其是第二篇)提供了您在此处尝试做的详细实施示例。 基本上诀窍是确定模板从变量扩展,因为你无法包装... {% extends '..' %}

...周围有{% if %}陈述。 工作实例 {% set template = 'MyBundl

...

“动态”不能在树枝中完成,因为在渲染之后无法改变输出。 在我看来,你可以通过两种方式做到: 使用twig在库中循环并输出它们,然后在更改选项时使用一些javascript(如引导选项卡)来显示每个库。 在bootstrap中你会这样做(假设gallery-> images关系在$ gallery-> images中):

{% for gallery in galleries %}

methods: POST

调节器 public function getFilteredListAction(Request $request)

{

$para

...

控制器应该看起来像我建议的那样: /**

* @Route("/myentity/{myentid}")

*/

public function newAndEditAction(Request $request, $myentid = null)

{

$em = $this->getDoctrine()->getManager();

// Here I determine if I have to create a form for a new entity or an

...

唯一错误的是你正在渲染索引页面。 成功登录后,您必须重定向您的页面 window.location = "http://somewhereelse.com";

或者您也可以在任何div中附加数据,它可以指向您想要遵循的方式。 The only wrong thing is this your are rendering the index page . after successfull login you have to redirect you page by window.location

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值