Fixed #3303 -- Cleaned some newforms clean() method implementations. Thanks for the patch, SmileyChris

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4367 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-01-20 17:31:45 +00:00
parent 5e8c1a6c47
commit 230b746f6c
1 changed files with 9 additions and 9 deletions

View File

@ -92,9 +92,7 @@ class CharField(Field):
"Validates max_length and min_length. Returns a Unicode object." "Validates max_length and min_length. Returns a Unicode object."
super(CharField, self).clean(value) super(CharField, self).clean(value)
if value in EMPTY_VALUES: if value in EMPTY_VALUES:
value = u'' return u''
if not self.required:
return value
value = smart_unicode(value) value = smart_unicode(value)
if self.max_length is not None and len(value) > self.max_length: if self.max_length is not None and len(value) > self.max_length:
raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length) raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length)
@ -117,7 +115,7 @@ class IntegerField(Field):
of int(). Returns None for empty values. of int(). Returns None for empty values.
""" """
super(IntegerField, self).clean(value) super(IntegerField, self).clean(value)
if not self.required and value in EMPTY_VALUES: if value in EMPTY_VALUES:
return None return None
try: try:
value = int(value) value = int(value)
@ -245,9 +243,10 @@ class RegexField(Field):
Unicode object. Unicode object.
""" """
super(RegexField, self).clean(value) super(RegexField, self).clean(value)
if value in EMPTY_VALUES: value = u'' if value in EMPTY_VALUES:
value = u''
value = smart_unicode(value) value = smart_unicode(value)
if not self.required and value == u'': if value == u'':
return value return value
if self.max_length is not None and len(value) > self.max_length: if self.max_length is not None and len(value) > self.max_length:
raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length) raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length)
@ -288,7 +287,7 @@ class URLField(RegexField):
def clean(self, value): def clean(self, value):
value = super(URLField, self).clean(value) value = super(URLField, self).clean(value)
if not self.required and value == u'': if value == u'':
return value return value
if self.verify_exists: if self.verify_exists:
import urllib2 import urllib2
@ -329,9 +328,10 @@ class ChoiceField(Field):
Validates that the input is in self.choices. Validates that the input is in self.choices.
""" """
value = super(ChoiceField, self).clean(value) value = super(ChoiceField, self).clean(value)
if value in EMPTY_VALUES: value = u'' if value in EMPTY_VALUES:
value = u''
value = smart_unicode(value) value = smart_unicode(value)
if not self.required and value == u'': if value == u'':
return value return value
valid_values = set([str(k) for k, v in self.choices]) valid_values = set([str(k) for k, v in self.choices])
if value not in valid_values: if value not in valid_values: