Php Symfony2 ajax细枝表单错误

Php Symfony2 ajax细枝表单错误,php,jquery,ajax,symfony,twig,Php,Jquery,Ajax,Symfony,Twig,有一个问题是,当表单提交错误从ajax响应返回时,如何在Symfony2中正确输出表单提交错误 我正在通过ajax发布一个表单,如果表单填写不正确,它将使用以下代码返回一个带有错误的回复 $errors = $form->getErrorsAsString(); $return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$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"}
 $("#errorReport").html(data.errorReport);
然后我使用jQuery将错误写入div,如下所示:

 {"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"}
 $("#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中是否存在我可以格式化这些错误的格式,以便在将这些错误传递回Twig模板时,它们看起来是可展示的?我希望它看起来像这样,但我不知道它是怎么做的:

 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)
非常感谢你的帮助

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

所以代码看起来像这样

$errors = $form->getErrors();
$errorCollection = array();
foreach($errors as $error){
       $errorCollection[] = $error->getMessageTemplate()
}
$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errorCollection);
您应该使用$form->getErrors方法,而不是$form->getErrorsString;getErrors函数返回FormError对象,该对象可用于创建错误消息

所以代码看起来像这样

$errors = $form->getErrors();
$errorCollection = array();
foreach($errors as $error){
       $errorCollection[] = $error->getMessageTemplate()
}
$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errorCollection);

我认为最干净的解决方案是实现JMSSerializerBundle,它使用以下类:

然后在控制器中:


我认为最干净的解决方案是实现JMSSerializerBundle,它使用以下类:

然后在控制器中:

$errors=strtr$form->getErrorsAsString,数组“\n”=>;”怎么样?$errors=strtr$form->getErrorsAsString,数组“\n”=>;”怎么样?