mirror of
https://github.com/django/django.git
synced 2025-07-07 03:09:22 +00:00
[soc2009/model-validation] Added min and max length validator. CharField now validates it's value's length using a validator.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11194 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0e3b83c315
commit
ff0eff055e
@ -40,6 +40,7 @@ def validate_ipv4_address(value):
|
|||||||
code="invalid"
|
code="invalid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class MaxValueValidator(object):
|
class MaxValueValidator(object):
|
||||||
def __init__(self, max_value):
|
def __init__(self, max_value):
|
||||||
self.max_value = max_value
|
self.max_value = max_value
|
||||||
@ -51,6 +52,7 @@ class MaxValueValidator(object):
|
|||||||
code='max_value',
|
code='max_value',
|
||||||
params=(self.max_value,)
|
params=(self.max_value,)
|
||||||
)
|
)
|
||||||
|
|
||||||
class MinValueValidator(object):
|
class MinValueValidator(object):
|
||||||
def __init__(self, min_value):
|
def __init__(self, min_value):
|
||||||
self.min_value = min_value
|
self.min_value = min_value
|
||||||
@ -63,6 +65,32 @@ class MinValueValidator(object):
|
|||||||
params=(self.min_value,)
|
params=(self.min_value,)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class MinLengthValidator(object):
|
||||||
|
def __init__(self, min_length):
|
||||||
|
self.min_length = min_length
|
||||||
|
|
||||||
|
def __call__(self, value):
|
||||||
|
value_len = len(value)
|
||||||
|
if value_len < self.min_length:
|
||||||
|
raise ValidationError(
|
||||||
|
_(u'Ensure this value has at least %(min)d characters (it has %(length)d).'),
|
||||||
|
code='min_length',
|
||||||
|
params={ 'min': self.min_length, 'length': value_len}
|
||||||
|
)
|
||||||
|
|
||||||
|
class MaxLengthValidator(object):
|
||||||
|
def __init__(self, max_length):
|
||||||
|
self.max_length = max_length
|
||||||
|
|
||||||
|
def __call__(self, value):
|
||||||
|
value_len = len(value)
|
||||||
|
if value_len > self.max_length:
|
||||||
|
raise ValidationError(
|
||||||
|
_(u'Ensure this value has at most %(max)d characters (it has %(length)d).'),
|
||||||
|
code='max_length',
|
||||||
|
params={ 'max': self.max_length, 'length': value_len}
|
||||||
|
)
|
||||||
|
|
||||||
class ComplexValidator(object):
|
class ComplexValidator(object):
|
||||||
def get_value(self, name, all_values, obj):
|
def get_value(self, name, all_values, obj):
|
||||||
assert all_values or obj, "Either all_values or obj must be supplied"
|
assert all_values or obj, "Either all_values or obj must be supplied"
|
||||||
|
@ -469,6 +469,10 @@ class BooleanField(Field):
|
|||||||
return super(BooleanField, self).formfield(**defaults)
|
return super(BooleanField, self).formfield(**defaults)
|
||||||
|
|
||||||
class CharField(Field):
|
class CharField(Field):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(CharField, self).__init__(*args, **kwargs)
|
||||||
|
self.validators.append(validators.MaxLengthValidator(self.max_length))
|
||||||
|
|
||||||
def get_internal_type(self):
|
def get_internal_type(self):
|
||||||
return "CharField"
|
return "CharField"
|
||||||
|
|
||||||
@ -478,6 +482,9 @@ class CharField(Field):
|
|||||||
return smart_unicode(value)
|
return smart_unicode(value)
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
|
# passing of max_length to forms.CharField means that the value's length
|
||||||
|
# will be validated twice. This is considered acceptable since we want
|
||||||
|
# the value in the form field (to pass into widget for example).
|
||||||
defaults = {'max_length': self.max_length}
|
defaults = {'max_length': self.max_length}
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
return super(CharField, self).formfield(**defaults)
|
return super(CharField, self).formfield(**defaults)
|
||||||
|
@ -172,6 +172,11 @@ class CharField(Field):
|
|||||||
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
||||||
self.max_length, self.min_length = max_length, min_length
|
self.max_length, self.min_length = max_length, min_length
|
||||||
super(CharField, self).__init__(*args, **kwargs)
|
super(CharField, self).__init__(*args, **kwargs)
|
||||||
|
# TODO: use this as soon as you make regex validator and use it in RegexField
|
||||||
|
#if min_length is not None:
|
||||||
|
# self.validators.append(validators.MinLengthValidator(min_length))
|
||||||
|
#if max_length is not None:
|
||||||
|
# self.validators.append(validators.MaxLengthValidator(max_length))
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
"Returns a Unicode object."
|
"Returns a Unicode object."
|
||||||
|
@ -39,6 +39,10 @@ class BaseModelValidationTests(ValidationTestCase):
|
|||||||
mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com')
|
mtv = ModelToValidate(number=10, name='Some Name', email='valid@email.com')
|
||||||
self.assertEqual(None, mtv.clean())
|
self.assertEqual(None, mtv.clean())
|
||||||
|
|
||||||
|
def test_text_greater_that_charfields_max_length_eaises_erros(self):
|
||||||
|
mtv = ModelToValidate(number=10, name='Some Name'*100)
|
||||||
|
self.assertFailsValidation(mtv.clean, ['name',])
|
||||||
|
|
||||||
class GetUniqueCheckTests(unittest.TestCase):
|
class GetUniqueCheckTests(unittest.TestCase):
|
||||||
def test_unique_fields_get_collected(self):
|
def test_unique_fields_get_collected(self):
|
||||||
m = UniqueFieldsModel()
|
m = UniqueFieldsModel()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user