Browse files

Fixed #2760 -- Fixed validation of negative float amounts in forms. T…

…hanks,

James Bennett and Eddy Mulyono.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3788 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 084d618 commit 14be60c4cb68ccd3062fc241ff2b9947fb010b98 @malcolmt malcolmt committed Sep 22, 2006
Showing with 5 additions and 3 deletions.
  1. +4 −2 django/core/validators.py
  2. +1 −1 django/forms/__init__.py
View
6 django/core/validators.py
@@ -351,10 +351,12 @@ def __call__(self, field_data, all_data):
float(data)
except ValueError:
raise ValidationError, gettext("Please enter a valid decimal number.")
- if len(data) > (self.max_digits + 1):
+ # Negative floats require more space to input.
+ max_allowed_length = data.startswith('-') and (self.max_digits + 2) or (self.max_digits + 1)
+ if len(data) > max_allowed_length:
raise ValidationError, ngettext("Please enter a valid decimal number with at most %s total digit.",
"Please enter a valid decimal number with at most %s total digits.", self.max_digits) % self.max_digits
- if (not '.' in data and len(data) > (self.max_digits - self.decimal_places)) or ('.' in data and len(data) > (self.max_digits - (self.decimal_places - len(data.split('.')[1])) + 1)):
+ if (not '.' in data and len(data) > (self.max_allowed_length - self.decimal_places)) or ('.' in data and len(data) > (self.max_digits - (self.decimal_places - len(data.split('.')[1])) + 1)):
raise ValidationError, ngettext( "Please enter a valid decimal number with a whole part of at most %s digit.",
"Please enter a valid decimal number with a whole part of at most %s digits.", str(self.max_digits-self.decimal_places)) % str(self.max_digits-self.decimal_places)
if '.' in data and len(data.split('.')[1]) > self.decimal_places:
View
2 django/forms/__init__.py
@@ -743,7 +743,7 @@ def __init__(self, field_name, max_digits, decimal_places, is_required=False, va
if validator_list is None: validator_list = []
self.max_digits, self.decimal_places = max_digits, decimal_places
validator_list = [self.isValidFloat] + validator_list
- TextField.__init__(self, field_name, max_digits+1, max_digits+1, is_required, validator_list)
+ TextField.__init__(self, field_name, max_digits+2, max_digits+2, is_required, validator_list)
def isValidFloat(self, field_data, all_data):
v = validators.IsValidFloat(self.max_digits, self.decimal_places)

0 comments on commit 14be60c

Please sign in to comment.