Fixed #20594 -- Add validation to models.SlugField.
Thanks carbonXT for the report.
This commit is contained in:
parent
675558d00e
commit
dc9c359546
|
@ -1211,6 +1211,7 @@ class PositiveSmallIntegerField(IntegerField):
|
||||||
return super(PositiveSmallIntegerField, self).formfield(**defaults)
|
return super(PositiveSmallIntegerField, self).formfield(**defaults)
|
||||||
|
|
||||||
class SlugField(CharField):
|
class SlugField(CharField):
|
||||||
|
default_validators = [validators.validate_slug]
|
||||||
description = _("Slug (up to %(max_length)s)")
|
description = _("Slug (up to %(max_length)s)")
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -1320,12 +1321,12 @@ class TimeField(Field):
|
||||||
return super(TimeField, self).formfield(**defaults)
|
return super(TimeField, self).formfield(**defaults)
|
||||||
|
|
||||||
class URLField(CharField):
|
class URLField(CharField):
|
||||||
|
default_validators = [validators.URLValidator()]
|
||||||
description = _("URL")
|
description = _("URL")
|
||||||
|
|
||||||
def __init__(self, verbose_name=None, name=None, **kwargs):
|
def __init__(self, verbose_name=None, name=None, **kwargs):
|
||||||
kwargs['max_length'] = kwargs.get('max_length', 200)
|
kwargs['max_length'] = kwargs.get('max_length', 200)
|
||||||
CharField.__init__(self, verbose_name, name, **kwargs)
|
CharField.__init__(self, verbose_name, name, **kwargs)
|
||||||
self.validators.append(validators.URLValidator())
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
# As with CharField, this will cause URL validation to be performed
|
# As with CharField, this will cause URL validation to be performed
|
||||||
|
|
|
@ -638,10 +638,7 @@ class URLField(CharField):
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _('Enter a valid URL.'),
|
'invalid': _('Enter a valid URL.'),
|
||||||
}
|
}
|
||||||
|
default_validators = [validators.URLValidator()]
|
||||||
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
|
||||||
super(URLField, self).__init__(max_length, min_length, *args, **kwargs)
|
|
||||||
self.validators.append(validators.URLValidator())
|
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ class ModelToValidate(models.Model):
|
||||||
email = models.EmailField(blank=True)
|
email = models.EmailField(blank=True)
|
||||||
url = models.URLField(blank=True)
|
url = models.URLField(blank=True)
|
||||||
f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe])
|
f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe])
|
||||||
|
slug = models.SlugField(blank=True)
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super(ModelToValidate, self).clean()
|
super(ModelToValidate, self).clean()
|
||||||
|
|
|
@ -53,7 +53,11 @@ class BaseModelValidationTests(ValidationTestCase):
|
||||||
|
|
||||||
def test_text_greater_that_charfields_max_length_raises_erros(self):
|
def test_text_greater_that_charfields_max_length_raises_erros(self):
|
||||||
mtv = ModelToValidate(number=10, name='Some Name'*100)
|
mtv = ModelToValidate(number=10, name='Some Name'*100)
|
||||||
self.assertFailsValidation(mtv.full_clean, ['name',])
|
self.assertFailsValidation(mtv.full_clean, ['name'])
|
||||||
|
|
||||||
|
def test_malformed_slug_raises_error(self):
|
||||||
|
mtv = ModelToValidate(number=10, name='Some Name', slug='##invalid##')
|
||||||
|
self.assertFailsValidation(mtv.full_clean, ['slug'])
|
||||||
|
|
||||||
|
|
||||||
class ArticleForm(forms.ModelForm):
|
class ArticleForm(forms.ModelForm):
|
||||||
|
|
Loading…
Reference in New Issue