summaryrefslogtreecommitdiff
path: root/django/forms/fields.py
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2009-07-05 13:27:20 +0000
committerHonza Král <honza.kral@gmail.com>2009-07-05 13:27:20 +0000
commitd23f541f102bc37e5ed9e78a05832335f3f3987b (patch)
tree17e9bad670ce1b2eb638bae933ad2f7e8f0e11bb /django/forms/fields.py
parent63f244f14413e0981c76dda8bf6f76e7b9df3d2a (diff)
[soc2009/model-validation] Make validators work with error messages containing paramters.
Implemented MaxValueValidator and MinValueValidator and migrated number based form.Fields to use those instead of hardwired code. The validators themselves are likely to change. Covered by existing tests git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11186 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms/fields.py')
-rw-r--r--django/forms/fields.py30
1 files changed, 14 insertions, 16 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index cff1970450..8fa66cf0d6 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -128,7 +128,10 @@ class Field(object):
v(value)
except ValidationError, e:
if hasattr(e, 'code'):
- errors.append(self.error_messages.get(e.code, e.messages[0]))
+ message = self.error_messages.get(e.code, e.messages[0])
+ if e.params:
+ message = message % e.params
+ errors.append(message)
else:
errors.extend(e.messages)
if errors:
@@ -201,9 +204,13 @@ class IntegerField(Field):
}
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
- self.max_value, self.min_value = max_value, min_value
super(IntegerField, self).__init__(*args, **kwargs)
+ if max_value is not None:
+ self.validators.append(validators.MaxValueValidator(max_value))
+ if min_value is not None:
+ self.validators.append(validators.MinValueValidator(min_value))
+
def to_python(self, value):
"""
Validates that int() can be called on the input. Returns the result
@@ -219,15 +226,6 @@ class IntegerField(Field):
raise ValidationError(self.error_messages['invalid'])
return value
- def validate(self, value):
- super(IntegerField, self).validate(value)
- if value in validators.EMPTY_VALUES:
- return
- if self.max_value is not None and value > self.max_value:
- raise ValidationError(self.error_messages['max_value'] % self.max_value)
- if self.min_value is not None and value < self.min_value:
- raise ValidationError(self.error_messages['min_value'] % self.min_value)
-
class FloatField(IntegerField):
default_error_messages = {
'invalid': _(u'Enter a number.'),
@@ -261,10 +259,14 @@ class DecimalField(Field):
}
def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs):
- self.max_value, self.min_value = max_value, min_value
self.max_digits, self.decimal_places = max_digits, decimal_places
Field.__init__(self, *args, **kwargs)
+ if max_value is not None:
+ self.validators.append(validators.MaxValueValidator(max_value))
+ if min_value is not None:
+ self.validators.append(validators.MinValueValidator(min_value))
+
def to_python(self, value):
"""
Validates that the input is a decimal number. Returns a Decimal
@@ -297,10 +299,6 @@ class DecimalField(Field):
digits = decimals
whole_digits = digits - decimals
- if self.max_value is not None and value > self.max_value:
- raise ValidationError(self.error_messages['max_value'] % self.max_value)
- if self.min_value is not None and value < self.min_value:
- raise ValidationError(self.error_messages['min_value'] % self.min_value)
if self.max_digits is not None and digits > self.max_digits:
raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
if self.decimal_places is not None and decimals > self.decimal_places: