将错误注入已验证的表单中? [英] Inject errors into already validated form?

查看:11
本文介绍了将错误注入已验证的表单中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 form.Form 验证用户输入值后,我将它们传递给单独的(外部)进程以进行进一步处理.此外部过程可能会发现值中的更多错误.

After my form.Form validates the user input values I pass them to a separate (external) process for further processing. This external process can potentially find further errors in the values.

有没有办法将这些错误注入已验证的表单中,以便可以通过通常的表单错误显示方法(或者有更好的替代方法)显示它们?

Is there a way to inject these errors into the already validated form so they can be displayed via the usual form error display methods (or are there better alternative approaches)?

一个建议是在表单验证中包含外部处理,这并不理想,因为外部处理不仅仅是验证.

One suggestions was to include the external processing in the form validation, which is not ideal because the external process does a lot more than merely validate.

推荐答案

Form._errors 可以像标准字典一样对待.使用 ErrorList 类并将错误附加到现有列表被认为是一种很好的形式:

Form._errors can be treated like a standard dictionary. It's considered good form to use the ErrorList class, and to append errors to the existing list:

from django.forms.utils import ErrorList
errors = form._errors.setdefault("myfield", ErrorList())
errors.append(u"My error here")

如果要添加非字段错误,请使用 django.forms.forms.NON_FIELD_ERRORS(默认为 "__all__")而不是 "myfield".

And if you want to add non-field errors, use django.forms.forms.NON_FIELD_ERRORS (defaults to "__all__") instead of "myfield".

这篇关于将错误注入已验证的表单中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆