Ticket #7755: django-models-validation-emailfield-blank.patch

File django-models-validation-emailfield-blank.patch, 2.3 KB (added by Harm Geerts, 16 years ago)
  • django/db/models/fields/__init__.py

     
    747747        return [oldforms.EmailField]
    748748
    749749    def validate(self, field_data, all_data):
    750         validators.isValidEmail(field_data, all_data)
     750        if field_data:
     751            validators.isValidEmail(field_data, all_data)
    751752
    752753    def formfield(self, **kwargs):
    753754        defaults = {'form_class': forms.EmailField}
  • tests/modeltests/validation/models.py

     
    1616    birthdate = models.DateField()
    1717    favorite_moment = models.DateTimeField()
    1818    email = models.EmailField()
     19    private_email = models.EmailField(blank=True)
    1920
    2021    def __unicode__(self):
    2122        return self.name
     
    130131>>> p.favorite_moment
    131132datetime.datetime(2002, 4, 3, 0, 0)
    132133
    133 >>> p = Person(**dict(valid_params, email='john@example.com'))
     134>>> p = Person(**dict(valid_params, email='', private_email=''))
    134135>>> p.validate()
     136{'email': [u'This field is required.']}
     137>>> p.email, p.private_email
     138('', '')
     139
     140>>> p = Person(**dict(valid_params, email='john@example.com', private_email='john@example.com'))
     141>>> p.validate()
    135142{}
    136 >>> p.email
    137 'john@example.com'
     143>>> p.email, p.private_email
     144('john@example.com', 'john@example.com')
    138145
    139 >>> p = Person(**dict(valid_params, email=u'john@example.com'))
     146>>> p = Person(**dict(valid_params, email=u'john@example.com', private_email=u'john@example.com'))
    140147>>> p.validate()
    141148{}
    142 >>> p.email
    143 u'john@example.com'
     149>>> p.email, p.private_email
     150(u'john@example.com', u'john@example.com')
    144151
    145 >>> p = Person(**dict(valid_params, email=22))
    146 >>> p.validate()['email']
    147 [u'Enter a valid e-mail address.']
     152>>> p = Person(**dict(valid_params, email=22, private_email=22))
     153>>> p.validate().values()
     154[[u'Enter a valid e-mail address.'], [u'Enter a valid e-mail address.']]
    148155
    149156# Make sure that Date and DateTime return validation errors and don't raise Python errors.
    150157>>> p = Person(name='John Doe', is_child=True, email='abc@def.com')
Back to Top