mirror of https://github.com/django/django.git
Fixed #26821 -- Fixed forms.Email/URLField crash on None value.
This commit is contained in:
parent
d44afd8892
commit
a7b5dfd170
|
@ -532,9 +532,8 @@ class EmailField(CharField):
|
|||
widget = EmailInput
|
||||
default_validators = [validators.validate_email]
|
||||
|
||||
def clean(self, value):
|
||||
value = self.to_python(value).strip()
|
||||
return super(EmailField, self).clean(value)
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(EmailField, self).__init__(*args, strip=True, **kwargs)
|
||||
|
||||
|
||||
class FileField(Field):
|
||||
|
@ -665,6 +664,9 @@ class URLField(CharField):
|
|||
}
|
||||
default_validators = [validators.URLValidator()]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(URLField, self).__init__(*args, strip=True, **kwargs)
|
||||
|
||||
def to_python(self, value):
|
||||
|
||||
def split_url(url):
|
||||
|
@ -696,10 +698,6 @@ class URLField(CharField):
|
|||
value = urlunsplit(url_fields)
|
||||
return value
|
||||
|
||||
def clean(self, value):
|
||||
value = self.to_python(value).strip()
|
||||
return super(URLField, self).clean(value)
|
||||
|
||||
|
||||
class BooleanField(Field):
|
||||
widget = CheckboxInput
|
||||
|
|
|
@ -334,6 +334,10 @@ Miscellaneous
|
|||
new :meth:`.AbstractUser.clean` method so that normalization is applied in
|
||||
cases like model form validation.
|
||||
|
||||
* ``EmailField`` and ``URLField`` no longer accept the ``strip`` keyword
|
||||
argument. Remove it because it doesn't have an effect in older versions of
|
||||
Django as these fields alway strip whitespace.
|
||||
|
||||
.. _deprecated-features-1.11:
|
||||
|
||||
Features deprecated in 1.11
|
||||
|
|
|
@ -51,3 +51,12 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
|||
self.assertEqual('alf@foo.com', f.clean('alf@foo.com'))
|
||||
with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 15 characters (it has 20).'"):
|
||||
f.clean('alf123456788@foo.com')
|
||||
|
||||
def test_emailfield_strip_on_none_value(self):
|
||||
f = EmailField(required=False, empty_value=None)
|
||||
self.assertIsNone(f.clean(None))
|
||||
|
||||
def test_emailfield_unable_to_set_strip_kwarg(self):
|
||||
msg = "__init__() got multiple values for keyword argument 'strip'"
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
EmailField(strip=False)
|
||||
|
|
|
@ -151,3 +151,12 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
|||
def test_urlfield_normalization(self):
|
||||
f = URLField()
|
||||
self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/')
|
||||
|
||||
def test_urlfield_strip_on_none_value(self):
|
||||
f = URLField(required=False, empty_value=None)
|
||||
self.assertIsNone(f.clean(None))
|
||||
|
||||
def test_urlfield_unable_to_set_strip_kwarg(self):
|
||||
msg = "__init__() got multiple values for keyword argument 'strip'"
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
URLField(strip=False)
|
||||
|
|
Loading…
Reference in New Issue