Fixed #16617 -- Added 'value' to BaseValidator params.

Also allowed overriding the default messages in subclasses of BaseValidator.

Thanks sperrygrove for initial patch.
This commit is contained in:
Anubhav Joshi 2014-07-24 23:31:12 +05:30 committed by Tim Graham
parent 5a311d2ccc
commit 5cdb8f8c1e
4 changed files with 34 additions and 8 deletions

View File

@ -238,12 +238,14 @@ class BaseValidator(object):
message = _('Ensure this value is %(limit_value)s (it is %(show_value)s).')
code = 'limit_value'
def __init__(self, limit_value):
def __init__(self, limit_value, message=None):
self.limit_value = limit_value
if message:
self.message = message
def __call__(self, value):
cleaned = self.clean(value)
params = {'limit_value': self.limit_value, 'show_value': cleaned}
params = {'limit_value': self.limit_value, 'show_value': cleaned, 'value': value}
if self.compare(cleaned, self.limit_value):
raise ValidationError(self.message, code=self.code, params=params)

View File

@ -171,28 +171,44 @@ to, or in lieu of custom ``field.clean()`` methods.
``MaxValueValidator``
---------------------
.. class:: MaxValueValidator(max_value)
.. class:: MaxValueValidator(max_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'max_value'`` if ``value`` is greater than ``max_value``.
.. versionchanged:: 1.8
The ``message`` parameter was added.
``MinValueValidator``
---------------------
.. class:: MinValueValidator(min_value)
.. class:: MinValueValidator(min_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'min_value'`` if ``value`` is less than ``min_value``.
.. versionchanged:: 1.8
The ``message`` parameter was added.
``MaxLengthValidator``
----------------------
.. class:: MaxLengthValidator(max_length)
.. class:: MaxLengthValidator(max_length, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'max_length'`` if the length of ``value`` is greater than ``max_length``.
.. versionchanged:: 1.8
The ``message`` parameter was added.
``MinLengthValidator``
----------------------
.. class:: MinLengthValidator(min_length)
.. class:: MinLengthValidator(min_length, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'min_length'`` if the length of ``value`` is less than ``min_length``.
.. versionchanged:: 1.8
The ``message`` parameter was added.

View File

@ -154,10 +154,12 @@ class FormsErrorMessagesTestCase(TestCase, AssertFormErrorsMixin):
e = {
'required': 'REQUIRED',
'invalid': 'INVALID',
'max_length': '"%(value)s" has more than %(limit_value)d characters.',
}
f = URLField(error_messages=e)
f = URLField(error_messages=e, max_length=17)
self.assertFormErrors(['REQUIRED'], f.clean, '')
self.assertFormErrors(['INVALID'], f.clean, 'abc.c')
self.assertFormErrors(['"http://djangoproject.com" has more than 17 characters.'], f.clean, 'djangoproject.com')
def test_booleanfield(self):
e = {

View File

@ -14,6 +14,7 @@ from django.core.validators import (
validate_ipv46_address, validate_ipv4_address, validate_ipv6_address,
validate_slug,
)
from django.test import SimpleTestCase
from django.test.utils import str_prefix
@ -243,7 +244,7 @@ def create_simple_test_method(validator, expected, value, num):
# Dynamically assemble a test class with the contents of TEST_DATA
class TestSimpleValidators(TestCase):
class TestSimpleValidators(SimpleTestCase):
def test_single_message(self):
v = ValidationError('Not Valid')
self.assertEqual(str(v), str_prefix("[%(_)s'Not Valid']"))
@ -267,6 +268,11 @@ class TestSimpleValidators(TestCase):
else:
self.fail("TypeError not raised when flags and pre-compiled regex in RegexValidator")
def test_max_length_validator_message(self):
v = MaxLengthValidator(16, message='"%(value)s" has more than %(limit_value)d characters.')
with self.assertRaisesMessage(ValidationError, '"djangoproject.com" has more than 16 characters.'):
v('djangoproject.com')
test_counter = 0
for validator, value, expected in TEST_DATA:
name, method = create_simple_test_method(validator, expected, value, test_counter)