Commit 9b8f966e by Tom Christie

Merge pull request #3073 from tomchristie/support_trim_whitespace_with_allow_blank_false

Support allow_blank=False with trim_whitespace=True.
parents d5609979 39ece9be
...@@ -580,7 +580,7 @@ class CharField(Field): ...@@ -580,7 +580,7 @@ class CharField(Field):
# Test for the empty string here so that it does not get validated, # Test for the empty string here so that it does not get validated,
# and so that subclasses do not need to handle it explicitly # and so that subclasses do not need to handle it explicitly
# inside the `to_internal_value()` method. # inside the `to_internal_value()` method.
if data == '': if data == '' or (self.trim_whitespace and six.text_type(data).strip() == ''):
if not self.allow_blank: if not self.allow_blank:
self.fail('blank') self.fail('blank')
return '' return ''
......
...@@ -461,6 +461,13 @@ class TestCharField(FieldValues): ...@@ -461,6 +461,13 @@ class TestCharField(FieldValues):
field = serializers.CharField(trim_whitespace=False) field = serializers.CharField(trim_whitespace=False)
assert field.to_internal_value(' abc ') == ' abc ' assert field.to_internal_value(' abc ') == ' abc '
def test_disallow_blank_with_trim_whitespace(self):
field = serializers.CharField(allow_blank=False, trim_whitespace=True)
with pytest.raises(serializers.ValidationError) as exc_info:
field.run_validation(' ')
assert exc_info.value.detail == ['This field may not be blank.']
class TestEmailField(FieldValues): class TestEmailField(FieldValues):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment