clean
方法是引发非字段错误的地方:
def clean(self):
cleaned_data = super(MyForm, self).clean()
if condition1(cleaned_data['f1'], cleaned_data['f2']):
raise ValidationError('Condition1 error')
if condition3(cleaned_data['f3'], cleaned_data['f4']):
raise ValidationError('Condition2 error')
但是,如果我想同时提出这两个建议,以便用户一次看到所有错误,而不必一一纠正,该怎么办?
这可能是由于字段错误将字典传递给ValidationError
,但非字段错误呢?
答案 0 :(得分:3)
您可以使用add_error
method表格来完成此操作:
if condition1(cleaned_data['f1'], cleaned_data['f2']):
self.add_error(None, ValidationError('Condition1 error'))
if condition3(cleaned_data['f3'], cleaned_data['f4']):
self.add_error(None, ValidationError('Condition2 error'))