我正在尝试将自定义验证添加为表单的一部分。
当 自愿日期显示类型 是指定数字时,我正在尝试触发自定义验证。但是,当我运行以下代码时,自愿日期显示类型值为无,我期待一个数字/数字。
我已阅读有关表单字段验证的 django 文档,但我看不到我的错误。
目前只有最后一个 else 条件被触发,因为值为 None。
有人可以指出我做错了什么吗?
这是我的 forms.py 文件中的代码:
class Meta:
model = VoluntaryDetails
fields = (
.......
'voluntary_date_display_type',
.......
)
def clean_voluntary_finish_date(self):
voluntary_display_type = self.cleaned_data.get('voluntary_display_type')
voluntary_start_date = self.cleaned_data.get('voluntary_start_date')
voluntary_finish_date = self.cleaned_data.get('voluntary_finish_date')
voluntary_date_display_type = self.cleaned_data.get('voluntary_date_display_type')
if voluntary_display_type == 0:
if voluntary_finish_date is not None and voluntary_start_date is not None:
if voluntary_start_date > voluntary_finish_date:
if voluntary_date_display_type == 2 or voluntary_date_display_type == 3:
raise forms.ValidationError(_("To Date must be after the From Date."))
elif voluntary_date_display_type == 4 or voluntary_date_display_type == 5:
raise forms.ValidationError(_("Finish Date must be after the Start Date."))
elif voluntary_date_display_type == 6 or voluntary_date_display_type == 7:
raise forms.ValidationError(_("End Date must be after the Begin Date."))
elif voluntary_date_display_type == 8:
raise forms.ValidationError(_("This Date must be after the other Date."))
elif voluntary_date_display_type == 9 or voluntary_date_display_type == 10:
raise forms.ValidationError(_("This Duration date must be after the other Duration date."))
else:
raise forms.ValidationError(_("Completion Date must be after the Commencement Date."))
return voluntary_finish_date