【发布时间】:2011-01-08 04:35:53
【问题描述】:
我在 clean 方法中一直在做这样的事情:
if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']:
raise forms.ValidationError('The type and organization do not match.')
if self.cleaned_data['start'] > self.cleaned_data['end']:
raise forms.ValidationError('The start date cannot be later than the end date.')
但这意味着表单一次只能引发其中一个错误。有没有办法让表单同时引发这两个错误?
编辑 #1: 上述任何解决方案都很棒,但希望在以下场景中也能工作的东西:
if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']:
raise forms.ValidationError('The type and organization do not match.')
if self.cleaned_data['start'] > self.cleaned_data['end']:
raise forms.ValidationError('The start date cannot be later than the end date.')
super(FooAddForm, self).clean()
其中 FooAddForm 是一个 ModelForm 并且具有也可能导致错误的独特约束。如果有人知道这样的事情,那就太好了......
【问题讨论】:
标签: django django-forms django-validation