Django

Code

Changeset 6067

Show
Ignore:
Timestamp:
09/08/07 14:24:46 (1 year ago)
Author:
gwilson
Message:

Fixed #5232 -- Fixed the validation of DecimalField so that the negative sign is not counted as a digit. Thanks, Andrew Durdin.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/validators.py

    r5895 r6067  
    424424            raise ValidationError, _("Please enter a valid decimal number.") 
    425425 
    426         pieces = str(val).split('.') 
     426        pieces = str(val).lstrip("-").split('.') 
    427427        decimals = (len(pieces) == 2) and len(pieces[1]) or 0 
    428428        digits = len(pieces[0]) 
  • django/trunk/django/newforms/fields.py

    r5861 r6067  
    191191        except DecimalException: 
    192192            raise ValidationError(ugettext('Enter a number.')) 
    193         pieces = str(value).split('.') 
     193        pieces = str(value).lstrip("-").split('.') 
    194194        decimals = (len(pieces) == 2) and len(pieces[1]) or 0 
    195195        digits = len(pieces[0]) 
  • django/trunk/tests/regressiontests/forms/tests.py

    r5876 r6067  
    11681168... 
    11691169ValidationError: [u'Ensure that there are no more than 2 digits before the decimal point.'] 
     1170>>> f.clean('-12.34') 
     1171Decimal("-12.34") 
     1172>>> f.clean('-123.45') 
     1173Traceback (most recent call last): 
     1174... 
     1175ValidationError: [u'Ensure that there are no more than 4 digits in total.'] 
     1176>>> f.clean('-.12') 
     1177Decimal("-0.12") 
     1178>>> f.clean('-00.12') 
     1179Decimal("-0.12") 
     1180>>> f.clean('-000.12') 
     1181Decimal("-0.12") 
     1182>>> f.clean('-000.123') 
     1183Traceback (most recent call last): 
     1184... 
     1185ValidationError: [u'Ensure that there are no more than 2 decimal places.'] 
     1186>>> f.clean('-000.1234') 
     1187Traceback (most recent call last): 
     1188... 
     1189ValidationError: [u'Ensure that there are no more than 4 digits in total.'] 
     1190>>> f.clean('--0.12') 
     1191Traceback (most recent call last): 
     1192... 
     1193ValidationError: [u'Enter a number.'] 
     1194 
    11701195>>> f = DecimalField(max_digits=4, decimal_places=2, required=False) 
    11711196>>> f.clean('')