Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions django/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ def __init__(self, limit_value, message=None):

def __call__(self, value):
cleaned = self.clean(value)
params = {'limit_value': self.limit_value, 'show_value': cleaned, 'value': value}
if self.compare(cleaned, self.limit_value):
limit_value = self.limit_value() if callable(self.limit_value) else self.limit_value
params = {'limit_value': limit_value, 'show_value': cleaned, 'value': value}
if self.compare(cleaned, limit_value):
raise ValidationError(self.message, code=self.code, params=params)

def __eq__(self, other):
Expand Down
28 changes: 24 additions & 4 deletions docs/ref/validators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,31 +236,51 @@ to, or in lieu of custom ``field.clean()`` methods.
.. class:: MaxValueValidator(limit_value, message=None)

Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'max_value'`` if ``value`` is greater than ``limit_value``.
``'max_value'`` if ``value`` is greater than ``limit_value``, which may be
a callable.

.. versionchanged:: 2.2

``limit_value`` can now be a callable.

``MinValueValidator``
---------------------

.. class:: MinValueValidator(limit_value, message=None)

Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'min_value'`` if ``value`` is less than ``limit_value``.
``'min_value'`` if ``value`` is less than ``limit_value``, which may be a
callable.

.. versionchanged:: 2.2

``limit_value`` can now be a callable.

``MaxLengthValidator``
----------------------

.. class:: MaxLengthValidator(limit_value, message=None)

Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'max_length'`` if the length of ``value`` is greater than ``limit_value``.
``'max_length'`` if the length of ``value`` is greater than
``limit_value``, which may be a callable.

.. versionchanged:: 2.2

``limit_value`` can now be a callable.

``MinLengthValidator``
----------------------

.. class:: MinLengthValidator(limit_value, message=None)

Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'min_length'`` if the length of ``value`` is less than ``limit_value``.
``'min_length'`` if the length of ``value`` is less than ``limit_value``,
which may be a callable.

.. versionchanged:: 2.2

``limit_value`` can now be a callable.

``DecimalValidator``
--------------------
Expand Down
4 changes: 3 additions & 1 deletion docs/releases/2.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ URLs
Validators
~~~~~~~~~~

* ...
* :class:`.MaxValueValidator`, :class:`.MinValueValidator`,
:class:`.MinLengthValidator`, and :class:`.MaxLengthValidator` now accept
a callable ``limit_value``.

.. _backwards-incompatible-2.2:

Expand Down
4 changes: 4 additions & 0 deletions tests/validators/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@
(MinValueValidator(0), -1, ValidationError),
(MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),

# limit_value may be a callable.
(MinValueValidator(lambda: 1), 0, ValidationError),
(MinValueValidator(lambda: 1), 1, None),

(MaxLengthValidator(10), '', None),
(MaxLengthValidator(10), 10 * 'x', None),

Expand Down