我在Django以下形式:Django的表單驗證錯誤
class UserEmailRegistrationForm(forms.Form):
password = forms.PasswordField(max_length=32, label=_('Password'),
validators=[validators.validate_password])
email = forms.EmailField(label=_('Email'),
validators=[validators.validate_email])
first_name = forms.CharField(max_length=35, label=_('First name'),
validators=[validators.validate_name])
last_name = forms.CharField(max_length=35, label=_('Last name'),
validators=[validators.validate_name])
大多數驗證是定製的,但validate_email
作爲見於django.core.validators
。另外,這裏是我的觀點的相關部分:
form = UserEmailRegistrationForm(PUT)
# Validate incoming data
if not form.is_valid():
return HttpResponse(form.errors.as_json(),
content_type='application/json', status=422)
所有驗證工作正常,但我得到Enter a valid email address.
兩次form.errors
當我沿着無效的電子郵件地址傳遞。這是我得到的迴應:
{"email": [{"message": "Enter a valid email address.", "code": "invalid"}, {"message": "Enter a valid email address.", "code": "invalid"}]}
什麼可能導致這種情況,我該如何解決它?
定了!但這是一種奇怪的行爲。也許這個參數應該被稱爲「extra_validators」,它會更合理。 – 2014-10-07 14:51:01