mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
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:
commit
9b8f966e06
|
@ -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 ''
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user