Fixed #32559 -- Added 'step_size’ to numeric form fields.

Co-authored-by: Jacob Rief <jacob.rief@uibk.ac.at>
This commit is contained in:
Kapil Bansal 2022-05-12 11:30:47 +02:00 committed by Carlton Gibson
parent 68da6b389c
commit 3a82b5f655
9 changed files with 137 additions and 19 deletions

View file

@ -492,18 +492,20 @@ For each field, we describe the default widget used if you don't specify
* Normalizes to: A Python ``decimal``.
* Validates that the given value is a decimal. Uses
:class:`~django.core.validators.MaxValueValidator` and
:class:`~django.core.validators.MinValueValidator` if ``max_value`` and
``min_value`` are provided. Leading and trailing whitespace is ignored.
:class:`~django.core.validators.MinValueValidator` if ``max_value`` and
``min_value`` are provided. Uses
:class:`~django.core.validators.StepValueValidator` if ``step_size`` is
provided. Leading and trailing whitespace is ignored.
* Error message keys: ``required``, ``invalid``, ``max_value``,
``min_value``, ``max_digits``, ``max_decimal_places``,
``max_whole_digits``
``max_whole_digits``, ``step_size``.
The ``max_value`` and ``min_value`` error messages may contain
``%(limit_value)s``, which will be substituted by the appropriate limit.
Similarly, the ``max_digits``, ``max_decimal_places`` and
``max_whole_digits`` error messages may contain ``%(max)s``.
Takes four optional arguments:
Takes five optional arguments:
.. attribute:: max_value
.. attribute:: min_value
@ -521,6 +523,14 @@ For each field, we describe the default widget used if you don't specify
The maximum number of decimal places permitted.
.. attribute:: step_size
Limit valid inputs to an integral multiple of ``step_size``.
.. versionchanged:: 4.1
The ``step_size`` argument was added.
``DurationField``
-----------------
@ -636,13 +646,25 @@ For each field, we describe the default widget used if you don't specify
* Validates that the given value is a float. Uses
:class:`~django.core.validators.MaxValueValidator` and
:class:`~django.core.validators.MinValueValidator` if ``max_value`` and
``min_value`` are provided. Leading and trailing whitespace is allowed,
as in Python's ``float()`` function.
``min_value`` are provided. Uses
:class:`~django.core.validators.StepValueValidator` if ``step_size`` is
provided. Leading and trailing whitespace is allowed, as in Python's
``float()`` function.
* Error message keys: ``required``, ``invalid``, ``max_value``,
``min_value``
``min_value``, ``step_size``.
Takes two optional arguments for validation, ``max_value`` and ``min_value``.
These control the range of values permitted in the field.
Takes three optional arguments:
.. attribute:: max_value
.. attribute:: min_value
These control the range of values permitted in the field.
.. attribute:: step_size
.. versionadded:: 4.1
Limit valid inputs to an integral multiple of ``step_size``.
``GenericIPAddressField``
-------------------------
@ -755,21 +777,30 @@ For each field, we describe the default widget used if you don't specify
* Validates that the given value is an integer. Uses
:class:`~django.core.validators.MaxValueValidator` and
:class:`~django.core.validators.MinValueValidator` if ``max_value`` and
``min_value`` are provided. Leading and trailing whitespace is allowed,
as in Python's ``int()`` function.
``min_value`` are provided. Uses
:class:`~django.core.validators.StepValueValidator` if ``step_size`` is
provided. Leading and trailing whitespace is allowed, as in Python's
``int()`` function.
* Error message keys: ``required``, ``invalid``, ``max_value``,
``min_value``
``min_value``, ``step_size``
The ``max_value`` and ``min_value`` error messages may contain
``%(limit_value)s``, which will be substituted by the appropriate limit.
The ``max_value``, ``min_value`` and ``step_size`` error messages may
contain ``%(limit_value)s``, which will be substituted by the appropriate
limit.
Takes two optional arguments for validation:
Takes three optional arguments for validation:
.. attribute:: max_value
.. attribute:: min_value
These control the range of values permitted in the field.
.. attribute:: step_size
.. versionadded:: 4.1
Limit valid inputs to an integral multiple of ``step_size``.
``JSONField``
-------------

View file

@ -333,3 +333,15 @@ to, or in lieu of custom ``field.clean()`` methods.
The error code used by :exc:`~django.core.exceptions.ValidationError`
if validation fails. Defaults to ``"null_characters_not_allowed"``.
``StepValueValidator``
----------------------
.. versionadded:: 4.1
.. class:: StepValueValidator(limit_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
``'step_size'`` if ``value`` is not an integral multiple of
``limit_value``, which can be a float, integer or decimal value or a
callable.