您好我无法找到有关charfield必须与另一个charfield匹配的验证的文档
例如,我想验证new_password必须与confirm_new_password匹配:
class ChangePassword(forms.Form):
new_password = forms.CharField(label='New Password', max_length=100, error_messages={'required': 'New password is required'}, widget=forms.PasswordInput())
confirm_new_password = forms.CharField(label='Confirm New Password', max_length=100, error_messages={'required': 'Confirm New password is required'}, widget=forms.PasswordInput())
https://docs.djangoproject.com/en/1.9/ref/forms/fields/#charfield
主要的似乎是required, max_length, min_length
?
答案 0 :(得分:-1)
要执行自定义表单验证,您需要覆盖clean()函数,如下所示:
class ChangePassword(forms.Form):
new_password = forms.CharField(label='New Password', max_length=100, error_messages={'required': 'New password is required'}, widget=forms.PasswordInput())
confirm_new_password = forms.CharField(label='Confirm New Password', max_length=100, error_messages={'required': 'Confirm New password is required'}, widget=forms.PasswordInput())
def clean(self):
password = self.cleaned_data['new_password']
confirm_password = self.cleaned_data['confirm_new_password']
if password != confirm_password:
raise forms.ValidationError('Your passwords do not match!')