Code

Ticket #5232: decimalfield.diff

File decimalfield.diff, 1.7 KB (added by Andrew Durdin <adurdin@…>, 7 years ago)

Patch

Line 
1Index: django/core/validators.py
2===================================================================
3--- django/core/validators.py   (revision 5992)
4+++ django/core/validators.py   (working copy)
5@@ -423,7 +423,7 @@
6         except DecimalException:
7             raise ValidationError, _("Please enter a valid decimal number.")
8 
9-        pieces = str(val).split('.')
10+        pieces = str(val).lstrip("-").split('.')
11         decimals = (len(pieces) == 2) and len(pieces[1]) or 0
12         digits = len(pieces[0])
13 
14Index: django/newforms/fields.py
15===================================================================
16--- django/newforms/fields.py   (revision 5992)
17+++ django/newforms/fields.py   (working copy)
18@@ -190,7 +190,7 @@
19             value = Decimal(value)
20         except DecimalException:
21             raise ValidationError(ugettext('Enter a number.'))
22-        pieces = str(value).split('.')
23+        pieces = str(value).lstrip("-").split('.')
24         decimals = (len(pieces) == 2) and len(pieces[1]) or 0
25         digits = len(pieces[0])
26         if self.max_value is not None and value > self.max_value:
27Index: tests/regressiontests/forms/tests.py
28===================================================================
29--- tests/regressiontests/forms/tests.py        (revision 5992)
30+++ tests/regressiontests/forms/tests.py        (working copy)
31@@ -1167,6 +1167,12 @@
32 Traceback (most recent call last):
33 ...
34 ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.']
35+>>> f.clean('-123.45')
36+Traceback (most recent call last):
37+...
38+ValidationError: [u'Ensure that there are no more than 4 digits in total.']
39+>>> f.clean('-12.34')
40+Decimal("-12.34")
41 >>> f = DecimalField(max_digits=4, decimal_places=2, required=False)
42 >>> f.clean('')
43