Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #7064: Made DemicmalField validation support max_digits…

… equal to decimal_places.

r9387 and r9388 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9389 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 21e0efcd64b7aa0ffdbb7254574644af6d7b7727 1 parent 8c31bb7
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:
22  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,18 +416,28 @@
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
+>>> f.clean('.002') == Decimal("0.002")
  432
+True
  433
+
  434
+>>> f = DecimalField(max_digits=2, decimal_places=2)
  435
+>>> f.clean('.01') == Decimal(".01")
  436
+True
  437
+>>> f.clean('1.1')
  438
+Traceback (most recent call last):
  439
+...
  440
+ValidationError: [u'Ensure that there are no more than 0 digits before the decimal point.']
431 441
 
432 442
 
433 443
 # DateField ###################################################################

0 notes on commit 21e0efc

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