2014-10-07 84 views
1

我在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"}]} 

什麼可能導致這種情況,我該如何解決它?

回答

3

調試/查看發生了什麼。在您的IDE點擊EmailField,並按照層級...

貴國碼:

email = forms.EmailField(label=_('Email'), 
         validators=[validators.validate_email]) 

2.EmailField:

class EmailField(CharField): 
    widget = EmailInput 
    default_validators = [validators.validate_email] 

3.CharField /什麼有趣的。

class CharField(Field): ... 

4. Field.__init__(...)

... 
self.validators = self.default_validators + validators 
super(Field, self).__init__() 

總之,你使用validate_email兩次。

+0

定了!但這是一種奇怪的行爲。也許這個參數應該被稱爲「extra_validators」,它會更合理。 – 2014-10-07 14:51:01

1

正如弗朗索瓦暗示的那樣,兩個與您的email字段相關的驗證器相同。第一個是默認設置,然後通過爲validators關鍵字參數(validators=[validators.validate_email])提供值來添加第二個。這個驗證器列表被附加到任何默認列表。因此你會看到2個驗證錯誤消息。

$ python manage.py shell 
>>> from django import forms 
>>> from django.core import validators 
>>> email = forms.EmailField(label=_('Email'), validators=[validators.validate_email]) 
>>> email.validators 
[<django.core.validators.EmailValidator object at 0x2bc0190>, <django.core.validators.EmailValidator object at 0x2bc0190>] 

所以你可以看到上面有2個註冊的驗證器。當窗體驗證都被執行:

>>> email.run_validators('[email protected]') 
>>> email.run_validators('abc') 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/home/mhawke/virtualenvs/django_1.6/lib/python2.7/site-packages/django/forms/fields.py", line 139, in run_validators 
    raise ValidationError(errors) 
ValidationError: [u'Enter a valid email address.', u'Enter a valid email address.'] 

要解決,只需聲明電子郵件字段不驗證:

>>> email = forms.EmailField(label=_('Email')) 
>>> email.validators 
[<django.core.validators.EmailValidator object at 0x2bc0190>] 
>>> email.run_validators('[email protected]') 
>>> email.run_validators('abc') 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/home/mhawke/virtualenvs/django_1.6/lib/python2.7/site-packages/django/forms/fields.py", line 139, in run_validators 
    raise ValidationError(errors) 
ValidationError: [u'Enter a valid email address.']