Skip to content

Loading…

Forms - FloatField and IntegerField raise errors when a user enters space in textboxes. #1023

Closed
wants to merge 2 commits into from

3 participants

@krisys

When a user keys in few spaces in a FloatField or IntegerField. Django fails to treat them as empty and tries to convert them to float and int respectively, leading to an unnecessary validation error. Users need to define custom fields and custom clean methods currently to overcome this issue.

Ticket: https://code.djangoproject.com/ticket/20298

@krisys krisys In case of IntegerField and FloatField, there is a possibility of use…
…rs entering a space in the textbox. int() and float() can handle strings like ' 4 ' and ' 3.4 '. Whereas string like - ' ' throw an exception. Currently such strings also escape the EMPTY_VALUE check (None, '', [], (), {}). This patch strips the value before we check for emptiness.
b684c6d
@apollo13
Django member

Please open a ticket before making pull requests. This pull request also misses tests.

@krisys krisys 1. Checking if the value is of type basestring before calling the str…
…ip method.

2. Adding Test cases for the corresponding IntegerField and FloatField.
00dd32f
@krisys

Sorry, I had opened a ticket, but I forgot to link it here. ( https://code.djangoproject.com/ticket/20298 )

I have updated the code to include the test cases. Thanks for letting me know about it. After adding the tests, i figured out that my code was resulting in an error. I have fixed it now.

Regards
Krishna Bharadwaj

@timgraham
Django member

Ticket has been closed as duplicate of another.

@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 21, 2013
  1. @krisys

    In case of IntegerField and FloatField, there is a possibility of use…

    krisys committed
    …rs entering a space in the textbox. int() and float() can handle strings like ' 4 ' and ' 3.4 '. Whereas string like - ' ' throw an exception. Currently such strings also escape the EMPTY_VALUE check (None, '', [], (), {}). This patch strips the value before we check for emptiness.
  2. @krisys

    1. Checking if the value is of type basestring before calling the str…

    krisys committed
    …ip method.
    
    2. Adding Test cases for the corresponding IntegerField and FloatField.
Showing with 6 additions and 0 deletions.
  1. +4 −0 django/forms/fields.py
  2. +2 −0 tests/forms_tests/tests/test_fields.py
View
4 django/forms/fields.py
@@ -240,6 +240,8 @@ def to_python(self, value):
Validates that int() can be called on the input. Returns the result
of int(). Returns None for empty values.
"""
+ if isinstance(value, basestring):
+ value = value.strip()
value = super(IntegerField, self).to_python(value)
if value in self.empty_values:
return None
@@ -271,6 +273,8 @@ def to_python(self, value):
Validates that float() can be called on the input. Returns the result
of float(). Returns None for empty values.
"""
+ if isinstance(value, basestring):
+ value = value.strip()
value = super(IntegerField, self).to_python(value)
if value in self.empty_values:
return None
View
2 tests/forms_tests/tests/test_fields.py
@@ -165,6 +165,7 @@ def test_integerfield_1(self):
def test_integerfield_2(self):
f = IntegerField(required=False)
self.assertEqual(None, f.clean(''))
+ self.assertEqual(None, f.clean(' '))
self.assertEqual('None', repr(f.clean('')))
self.assertEqual(None, f.clean(None))
self.assertEqual('None', repr(f.clean(None)))
@@ -249,6 +250,7 @@ def test_floatfield_1(self):
def test_floatfield_2(self):
f = FloatField(required=False)
self.assertEqual(None, f.clean(''))
+ self.assertEqual(None, f.clean(' '))
self.assertEqual(None, f.clean(None))
self.assertEqual(1.0, f.clean('1'))
self.assertEqual(f.max_value, None)
Something went wrong with that request. Please try again.