Fixed #5232 -- Fixed the validation of `DecimalField` so that the negative 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
This commit is contained in:
Gary Wilson Jr 2007-09-08 19:24:46 +00:00
parent ef088d5f36
commit 7e57576ff7
3 changed files with 27 additions and 2 deletions

View File

@ -423,7 +423,7 @@ class IsValidDecimal(object):
except DecimalException: except DecimalException:
raise ValidationError, _("Please enter a valid decimal number.") 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 decimals = (len(pieces) == 2) and len(pieces[1]) or 0
digits = len(pieces[0]) digits = len(pieces[0])

View File

@ -190,7 +190,7 @@ class DecimalField(Field):
value = Decimal(value) value = Decimal(value)
except DecimalException: except DecimalException:
raise ValidationError(ugettext('Enter a number.')) 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 decimals = (len(pieces) == 2) and len(pieces[1]) or 0
digits = len(pieces[0]) digits = len(pieces[0])
if self.max_value is not None and value > self.max_value: if self.max_value is not None and value > self.max_value:

View File

@ -1167,6 +1167,31 @@ ValidationError: [u'Ensure that there are no more than 2 decimal places.']
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.'] 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 = DecimalField(max_digits=4, decimal_places=2, required=False)
>>> f.clean('') >>> f.clean('')