Ticket #2366: formsFloatField.diff

File formsFloatField.diff, 2.2 KB (added by adurdin@…, 9 years ago)

Patch

  • django/forms/__init__.py

     
    740740    def __init__(self, field_name, max_digits, decimal_places, is_required=False, validator_list=None):
    741741        if validator_list is None: validator_list = []
    742742        self.max_digits, self.decimal_places = max_digits, decimal_places
    743         validator_list = [self.isValidFloat] + validator_list
     743        validator_list = [self.isValidDecimal] + validator_list
    744744        TextField.__init__(self, field_name, max_digits+1, max_digits+1, is_required, validator_list)
    745745
    746     def isValidFloat(self, field_data, all_data):
    747         v = validators.IsValidFloat(self.max_digits, self.decimal_places)
     746    def isValidDecimal(self, field_data, all_data):
     747        v = validators.IsValidDecimal(self.max_digits, self.decimal_places)
    748748        try:
    749749            v(field_data, all_data)
    750750        except validators.ValidationError, e:
    751751            raise validators.CriticalValidationError, e.messages
    752752
    753753    def html2python(data):
     754        import decimal
    754755        if data == '' or data is None:
    755756            return None
    756         return float(data)
     757        return decimal.Decimal(data)
    757758    html2python = staticmethod(html2python)
    758759
    759760####################
  • django/core/validators.py

     
    342342        if val != int(val):
    343343            raise ValidationError, gettext("This value must be a power of %s.") % self.power_of
    344344
    345 class IsValidFloat(object):
     345class IsValidDecimal(object):
    346346    def __init__(self, max_digits, decimal_places):
    347347        self.max_digits, self.decimal_places = max_digits, decimal_places
    348348
    349349    def __call__(self, field_data, all_data):
     350        import decimal
    350351        data = str(field_data)
    351352        try:
    352             float(data)
     353            decimal.Decimal(data)
    353354        except ValueError:
    354355            raise ValidationError, gettext("Please enter a valid decimal number.")
    355356        if len(data) > (self.max_digits + 1):
Back to Top