Ticket #2366: formsFloatField.diff
File formsFloatField.diff, 2.2 KB (added by , 18 years ago) |
---|
-
django/forms/__init__.py
740 740 def __init__(self, field_name, max_digits, decimal_places, is_required=False, validator_list=None): 741 741 if validator_list is None: validator_list = [] 742 742 self.max_digits, self.decimal_places = max_digits, decimal_places 743 validator_list = [self.isValid Float] + validator_list743 validator_list = [self.isValidDecimal] + validator_list 744 744 TextField.__init__(self, field_name, max_digits+1, max_digits+1, is_required, validator_list) 745 745 746 def isValid Float(self, field_data, all_data):747 v = validators.IsValid Float(self.max_digits, self.decimal_places)746 def isValidDecimal(self, field_data, all_data): 747 v = validators.IsValidDecimal(self.max_digits, self.decimal_places) 748 748 try: 749 749 v(field_data, all_data) 750 750 except validators.ValidationError, e: 751 751 raise validators.CriticalValidationError, e.messages 752 752 753 753 def html2python(data): 754 import decimal 754 755 if data == '' or data is None: 755 756 return None 756 return float(data)757 return decimal.Decimal(data) 757 758 html2python = staticmethod(html2python) 758 759 759 760 #################### -
django/core/validators.py
342 342 if val != int(val): 343 343 raise ValidationError, gettext("This value must be a power of %s.") % self.power_of 344 344 345 class IsValid Float(object):345 class IsValidDecimal(object): 346 346 def __init__(self, max_digits, decimal_places): 347 347 self.max_digits, self.decimal_places = max_digits, decimal_places 348 348 349 349 def __call__(self, field_data, all_data): 350 import decimal 350 351 data = str(field_data) 351 352 try: 352 float(data)353 decimal.Decimal(data) 353 354 except ValueError: 354 355 raise ValidationError, gettext("Please enter a valid decimal number.") 355 356 if len(data) > (self.max_digits + 1):