Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 14be60c4cb68ccd3062fc241ff2b9947fb010b98 1 parent 084d618
Malcolm Tredinnick authored September 22, 2006
6  django/core/validators.py
@@ -351,10 +351,12 @@ def __call__(self, field_data, all_data):
351 351
             float(data)
352 352
         except ValueError:
353 353
             raise ValidationError, gettext("Please enter a valid decimal number.")
354  
-        if len(data) > (self.max_digits + 1):
  354
+        # Negative floats require more space to input.
  355
+        max_allowed_length = data.startswith('-') and (self.max_digits + 2) or (self.max_digits + 1)
  356
+        if len(data) > max_allowed_length:
355 357
             raise ValidationError, ngettext("Please enter a valid decimal number with at most %s total digit.",
356 358
                 "Please enter a valid decimal number with at most %s total digits.", self.max_digits) % self.max_digits
357  
-        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)):
  359
+        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)):
358 360
             raise ValidationError, ngettext( "Please enter a valid decimal number with a whole part of at most %s digit.",
359 361
                 "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)
360 362
         if '.' in data and len(data.split('.')[1]) > self.decimal_places:
2  django/forms/__init__.py
@@ -743,7 +743,7 @@ def __init__(self, field_name, max_digits, decimal_places, is_required=False, va
743 743
         if validator_list is None: validator_list = []
744 744
         self.max_digits, self.decimal_places = max_digits, decimal_places
745 745
         validator_list = [self.isValidFloat] + validator_list
746  
-        TextField.__init__(self, field_name, max_digits+1, max_digits+1, is_required, validator_list)
  746
+        TextField.__init__(self, field_name, max_digits+2, max_digits+2, is_required, validator_list)
747 747
 
748 748
     def isValidFloat(self, field_data, all_data):
749 749
         v = validators.IsValidFloat(self.max_digits, self.decimal_places)

0 notes on commit 14be60c

Please sign in to comment.
Something went wrong with that request. Please try again.