duanbicheng3345 2014-06-14 15:14
浏览 49

如何参考无效字段获取Symfony2表单验证错误列表?

As Symfony2 Form component with tight integration with the validation component is extremely powerful, I have hard times understanding how I can get the list of validation errors at PHP side with proper reference to the invalid field. By "reference", I mean some hint that I can further get the field and it's ID (like with $form->get($failedChild)->vars['id'] or something).

$form->getErrors() returns the list of the errors, but this array of FormErrors does not include any reference to the field that is invalid.

I found a Gist that shows something like that, but at least on 2.3 or 2.4 all errors are still bound to the main form.

error_bubbling option does not change the behavior as I first thought it would.


Update #1

In the end, I'd like to receive a JSON representation of all form errors and return it back to the client. A sample JSON would be something like

{
    "invalid_field_id": "This field has invalid value"
}

The ID of the field can be received over $field->vars['id'] but it seems that all validation errors are returned in $form->getErrors() instead of $field->getErrors()

  • 写回答

1条回答 默认 最新

  • doudansui6650 2014-06-14 20:08
    关注

    You can use $form->getErrorsAsString() to get a human-readable representation of your errors. These errors are mapped to fields, unless the error can not be attached to a field, or error_bubbling is set to true.

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程