summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/core/validators.py18
-rw-r--r--django/forms/fields.py14
-rw-r--r--tests/modeltests/validators/tests.py33
3 files changed, 54 insertions, 11 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index 1426c1c903..6758a387b0 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -22,6 +22,24 @@ def validate_email(value):
if not email_re.search(smart_unicode(value)):
raise ValidationError(_(u'Enter a valid e-mail address.'), code='invalid')
+slug_re = re.compile(r'^[-\w]+$')
+
+def validate_slug(value):
+ if not slug_re.search(smart_unicode(value)):
+ raise ValidationError(
+ _(u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."),
+ code='invalid'
+ )
+
+ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
+
+def validate_ipv4_address(value):
+ if not ipv4_re.search(smart_unicode(value)):
+ raise ValidationError(
+ _(u'Enter a valid IPv4 address.'),
+ code="invalid"
+ )
+
class MaxValueValidator(object):
def __init__(self, max_value):
self.max_value = max_value
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 9a037faca3..02e80456ab 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -914,23 +914,17 @@ class SplitDateTimeField(MultiValueField):
return datetime.datetime.combine(*data_list)
return None
-ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
-class IPAddressField(RegexField):
+class IPAddressField(CharField):
default_error_messages = {
'invalid': _(u'Enter a valid IPv4 address.'),
}
+ default_validators = [validators.validate_ipv4_address]
- def __init__(self, *args, **kwargs):
- super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs)
-
-slug_re = re.compile(r'^[-\w]+$')
-class SlugField(RegexField):
+class SlugField(CharField):
default_error_messages = {
'invalid': _(u"Enter a valid 'slug' consisting of letters, numbers,"
u" underscores or hyphens."),
}
-
- def __init__(self, *args, **kwargs):
- super(SlugField, self).__init__(slug_re, *args, **kwargs)
+ default_validators = [validators.validate_slug]
diff --git a/tests/modeltests/validators/tests.py b/tests/modeltests/validators/tests.py
index 7f84475149..e19353d03b 100644
--- a/tests/modeltests/validators/tests.py
+++ b/tests/modeltests/validators/tests.py
@@ -1,7 +1,11 @@
+# -*- coding: utf-8 -*-
from unittest import TestCase
from django.core.exceptions import ValidationError
-from django.core.validators import validate_integer, validate_email, RequiredIfOtherFieldBlank
+from django.core.validators import (
+ validate_integer, validate_email, RequiredIfOtherFieldBlank,
+ validate_slug, validate_ipv4_address
+ )
class TestSimpleValidators(TestCase):
pass
@@ -12,15 +16,42 @@ SIMPLE_VALIDATORS_VALUES = (
(validate_integer, '-42', None),
(validate_integer, -42, None),
(validate_integer, -42.5, None),
+
(validate_integer, None, ValidationError),
(validate_integer, 'a', ValidationError),
+
+
(validate_email, 'email@here.com', None),
(validate_email, 'weirder-email@here.and.there.com', None),
+
(validate_email, None, ValidationError),
(validate_email, '', ValidationError),
(validate_email, 'abc', ValidationError),
(validate_email, 'a @x.cz', ValidationError),
(validate_email, 'something@@somewhere.com', ValidationError),
+
+
+ (validate_slug, 'slug-ok', None),
+ (validate_slug, 'longer-slug-still-ok', None),
+ (validate_slug, '--------', None),
+ (validate_slug, 'nohyphensoranything', None),
+
+ (validate_slug, '', ValidationError),
+ (validate_slug, ' text ', ValidationError),
+ (validate_slug, ' ', ValidationError),
+ (validate_slug, 'some@mail.com', ValidationError),
+ (validate_slug, '你好', ValidationError),
+ (validate_slug, '\n', ValidationError),
+
+
+ (validate_ipv4_address, '1.1.1.1', None),
+ (validate_ipv4_address, '255.0.0.0', None),
+ (validate_ipv4_address, '0.0.0.0', None),
+
+ (validate_ipv4_address, '256.1.1.1', ValidationError),
+ (validate_ipv4_address, '25.1.1.', ValidationError),
+ (validate_ipv4_address, '25,1,1,1', ValidationError),
+ (validate_ipv4_address, '25.1 .1.1', ValidationError),
)
def get_simple_test_func(validator, expected, value, num):