Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7064: Made DemicmalField validation support max_digits equal t…

…o decimal_places.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9387 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d82aaef8446c4188a27ba4347b75f0dffaa42f0a 1 parent 8cdc53a
Karen Tracey authored November 10, 2008
10  django/forms/fields.py
@@ -254,12 +254,12 @@ def clean(self, value):
254 254
         decimals = abs(exponent)
255 255
         # digittuple doesn't include any leading zeros.
256 256
         digits = len(digittuple)
257  
-        if decimals >= digits:
  257
+        if decimals > digits:
258 258
             # We have leading zeros up to or past the decimal point.  Count
259  
-            # everything past the decimal point as a digit.  We also add one
260  
-            # for leading zeros before the decimal point (any number of leading
261  
-            # whole zeros collapse to one digit).
262  
-            digits = decimals + 1
  259
+            # everything past the decimal point as a digit.  We do not count 
  260
+            # 0 before the decimal point as a digit since that would mean 
  261
+            # we would not allow max_digits = decimal_places.
  262
+            digits = decimals
263 263
         whole_digits = digits - decimals
264 264
 
265 265
         if self.max_value is not None and value > self.max_value:
15  tests/regressiontests/forms/fields.py
@@ -366,7 +366,7 @@
366 366
 Traceback (most recent call last):
367 367
 ...
368 368
 ValidationError: [u'Ensure that there are no more than 2 decimal places.']
369  
->>> f.clean('-000.1234')
  369
+>>> f.clean('-000.12345')
370 370
 Traceback (most recent call last):
371 371
 ...
372 372
 ValidationError: [u'Ensure that there are no more than 4 digits in total.']
@@ -416,19 +416,20 @@
416 416
 # Leading whole zeros "collapse" to one digit.
417 417
 >>> f.clean('0000000.10') == Decimal("0.1")
418 418
 True
419  
->>> f.clean('0000000.100')
420  
-Traceback (most recent call last):
421  
-...
422  
-ValidationError: [u'Ensure that there are no more than 3 digits in total.']
  419
+
  420
+# But a leading 0 before the . doesn't count towards max_digits
  421
+>>> f.clean('0000000.100') == Decimal("0.100")
  422
+True
423 423
 
424 424
 # Only leading whole zeros "collapse" to one digit.
425 425
 >>> f.clean('000000.02') == Decimal('0.02')
426 426
 True
427  
->>> f.clean('000000.002')
  427
+>>> f.clean('000000.0002')
428 428
 Traceback (most recent call last):
429 429
 ...
430 430
 ValidationError: [u'Ensure that there are no more than 3 digits in total.']
431  
-
  431
+>>> f.clean('.002') == Decimal("0.002")
  432
+True
432 433
 
433 434
 # DateField ###################################################################
434 435
 

0 notes on commit d82aaef

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