Merge pull request #3073 from tomchristie/support_trim_whitespace_with_allow_blank_false

Support allow_blank=False with trim_whitespace=True.
This commit is contained in:
Tom Christie 2015-06-24 13:43:12 +01:00
commit 9b8f966e06
2 changed files with 8 additions and 1 deletions

View File

@ -580,7 +580,7 @@ class CharField(Field):
# Test for the empty string here so that it does not get validated,
# and so that subclasses do not need to handle it explicitly
# 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:
self.fail('blank')
return ''

View File

@ -461,6 +461,13 @@ class TestCharField(FieldValues):
field = serializers.CharField(trim_whitespace=False)
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):
"""