Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #5232 -- Fixed the validation of `DecimalField` so that the neg…

…ative sign is not counted as a digit. Thanks, Andrew Durdin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6067 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7e57576ff7fcad814c7a86de12d933a219106f8f 1 parent ef088d5
@gdub gdub authored
View
2  django/core/validators.py
@@ -423,7 +423,7 @@ def __call__(self, field_data, all_data):
except DecimalException:
raise ValidationError, _("Please enter a valid decimal number.")
- pieces = str(val).split('.')
+ pieces = str(val).lstrip("-").split('.')
decimals = (len(pieces) == 2) and len(pieces[1]) or 0
digits = len(pieces[0])
View
2  django/newforms/fields.py
@@ -190,7 +190,7 @@ def clean(self, value):
value = Decimal(value)
except DecimalException:
raise ValidationError(ugettext('Enter a number.'))
- pieces = str(value).split('.')
+ pieces = str(value).lstrip("-").split('.')
decimals = (len(pieces) == 2) and len(pieces[1]) or 0
digits = len(pieces[0])
if self.max_value is not None and value > self.max_value:
View
25 tests/regressiontests/forms/tests.py
@@ -1167,6 +1167,31 @@
Traceback (most recent call last):
...
ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.']
+>>> f.clean('-12.34')
+Decimal("-12.34")
+>>> f.clean('-123.45')
+Traceback (most recent call last):
+...
+ValidationError: [u'Ensure that there are no more than 4 digits in total.']
+>>> f.clean('-.12')
+Decimal("-0.12")
+>>> f.clean('-00.12')
+Decimal("-0.12")
+>>> f.clean('-000.12')
+Decimal("-0.12")
+>>> f.clean('-000.123')
+Traceback (most recent call last):
+...
+ValidationError: [u'Ensure that there are no more than 2 decimal places.']
+>>> f.clean('-000.1234')
+Traceback (most recent call last):
+...
+ValidationError: [u'Ensure that there are no more than 4 digits in total.']
+>>> f.clean('--0.12')
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a number.']
+
>>> f = DecimalField(max_digits=4, decimal_places=2, required=False)
>>> f.clean('')
Please sign in to comment.
Something went wrong with that request. Please try again.