## 11.6 通过 Form.add_error() 添加错误到表单
Michael Barr与我们分享,自从Django 1.7开始我们可以通过Form.add error()方法简化Form.clean()。
EXAMPLE 11.8from django import formsclass IceCreamReviewForm(forms.Form):# Rest of tester form goes here# 表单测试的其余部分...def clean(self):cleaned_data = super(TasterForm, self).clean()flavor = cleaned_data.get("flavor")age = cleaned_data.get("age")if flavor == 'coffee' and age < 3:# Record errors that will be displayed later.# 稍后将显示错误记录。msg = u"Coffee Ice Cream is not for Babies."self.add_error('flavor', msg)self.add_error('age', msg)# Always return the full collection of cleaned data.# 总是返回完整的清理的数据的收集。return cleaned_data
11.6.1 其他的实用表单方法
这里有些值得探索的表单验证方法:
http://www.2scoops.co/1.8-form.errors.as_data/
http://www.2scoops.co/1.8-form.errors.as_json/
