Since the HTML standard offers a `step` attribute on input fields of
`type="number"`,
this feature shall be reflected by Django's `FloatField` and optionally
`DecimalField`,
rather than having to parametrize the widget.
Min- and max-values are already supported by the `FloatField`, so the
step-value
would make sense here as well. It furthermore would require to revalidate
the
step-value by Django's Form validation, rather than by HTML alone.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
Thank you. Sounds like a perfectly valid use case.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:1>
Comment (by Tim Graham):
[https://groups.google.com/g/django-
developers/c/oVWKJUXTb1o/m/b1Oo0zqeAwAJ django-developers discussion]
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:2>
* owner: nobody => devkapilbansal
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:3>
* has_patch: 0 => 1
Old description:
> If someone wants to use the `step` attribute as provided by the HTML
> field
> `<input type="number" ...>` , she/he has to specify that using for
> instance
> `FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
>
> Since the HTML standard offers a `step` attribute on input fields of
> `type="number"`,
> this feature shall be reflected by Django's `FloatField` and optionally
> `DecimalField`,
> rather than having to parametrize the widget.
>
> Min- and max-values are already supported by the `FloatField`, so the
> step-value
> would make sense here as well. It furthermore would require to revalidate
> the
> step-value by Django's Form validation, rather than by HTML alone.
New description:
If someone wants to use the `step` attribute as provided by the HTML field
`<input type="number" ...>` , she/he has to specify that using for
instance
`FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
Since the HTML standard offers a `step` attribute on input fields of
`type="number"`,
this feature shall be reflected by Django's `FloatField` and optionally
`DecimalField`,
rather than having to parametrize the widget.
Min- and max-values are already supported by the `FloatField`, so the
step-value
would make sense here as well. It furthermore would require to revalidate
the
step-value by Django's Form validation, rather than by HTML alone.
Patch: https://github.com/django/django/pull/14162
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:4>
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:5>
Old description:
> If someone wants to use the `step` attribute as provided by the HTML
> field
> `<input type="number" ...>` , she/he has to specify that using for
> instance
> `FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
>
> Since the HTML standard offers a `step` attribute on input fields of
> `type="number"`,
> this feature shall be reflected by Django's `FloatField` and optionally
> `DecimalField`,
> rather than having to parametrize the widget.
>
> Min- and max-values are already supported by the `FloatField`, so the
> step-value
> would make sense here as well. It furthermore would require to revalidate
> the
> step-value by Django's Form validation, rather than by HTML alone.
>
> Patch: https://github.com/django/django/pull/14162
New description:
If someone wants to use the `step` attribute as provided by the HTML field
`<input type="number" ...>` , she/he has to specify that using for
instance
`FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
Since the HTML standard offers a `step` attribute on input fields of
`type="number"`,
this feature shall be reflected by Django's `FloatField` and optionally
`DecimalField`,
rather than having to parametrize the widget.
Min- and max-values are already supported by the `FloatField`, so the
step-value
would make sense here as well. It furthermore would require to revalidate
the
step-value by Django's Form validation, rather than by HTML alone.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:6>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:7>
* needs_tests: 1 => 0
Old description:
> If someone wants to use the `step` attribute as provided by the HTML
> field
> `<input type="number" ...>` , she/he has to specify that using for
> instance
> `FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
>
> Since the HTML standard offers a `step` attribute on input fields of
> `type="number"`,
> this feature shall be reflected by Django's `FloatField` and optionally
> `DecimalField`,
> rather than having to parametrize the widget.
>
> Min- and max-values are already supported by the `FloatField`, so the
> step-value
> would make sense here as well. It furthermore would require to revalidate
> the
> step-value by Django's Form validation, rather than by HTML alone.
New description:
If someone wants to use the `step` attribute as provided by the HTML field
`<input type="number" ...>` , she/he has to specify that using for
instance
`FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
Since the HTML standard offers a `step` attribute on input fields of
`type="number"`,
this feature shall be reflected by Django's `FloatField` and optionally
`DecimalField`,
rather than having to parametrize the widget.
Min- and max-values are already supported by the `FloatField`, so the
step-value
would make sense here as well. It furthermore would require to revalidate
the
step-value by Django's Form validation, rather than by HTML alone.
Patch: https://github.com/django/django/pull/14162
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:8>
Comment (by Adam Johnson):
On [https://forum.djangoproject.com/t/fixed-32559-add-attribute-step-to-
floatfield/7148 the forum], Kapil wrote:
> Hi,
> I was working on ticket 32559 to add step in FloatField but how to write
validation check for this.
> Due to python floating point issues, I am not able to validate whether
field value is of given step_size or not
The loss of precision in floating points is a real issue. The normal way
to validate would be to use the modulo operator to check there is no
remainder after dividing by the step, but this isn't possible in floating
points with common decimal steps like 0.1:
{{{
>>> step = 0.1
>>> 1 % step == 0
False
>>> 1 % step
0.09999999999999995
}}}
This problem cannot be solved for `FloatField` - I suggest we move the
ticket to modify `DecimalField`, as `Decimal` objects do not have the same
problem:
{{{
>>> from decimal import Decimal
>>> step = Decimal('0.1')
>>> Decimal(1) % step == 0
True
>>> Decimal(1) % step
Decimal('0.0')
}}}
We'd want to enforce that `step` is given as a `Decimal`.
The `float` problem can also occur when constructing a `Decimal` from a
`float`:
{{{
>>> Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
}}}
Perhaps we should have a check that the given step does not have more than
10 digits after the decimal point?
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:9>
Comment (by Kapil Bansal):
Hi,
I got a reply on mailing list to use math.isclose function. Pull Request
is already opened to be reviewed
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:10>
Old description:
> If someone wants to use the `step` attribute as provided by the HTML
> field
> `<input type="number" ...>` , she/he has to specify that using for
> instance
> `FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
>
> Since the HTML standard offers a `step` attribute on input fields of
> `type="number"`,
> this feature shall be reflected by Django's `FloatField` and optionally
> `DecimalField`,
> rather than having to parametrize the widget.
>
> Min- and max-values are already supported by the `FloatField`, so the
> step-value
> would make sense here as well. It furthermore would require to revalidate
> the
> step-value by Django's Form validation, rather than by HTML alone.
>
> Patch: https://github.com/django/django/pull/14162
New description:
I reviewed that pull request and after some testing I came to the
conclusion, that `math.isclose` with a tolerance of `1e-9` is the best
solution to fix the floating point rounding errors. All other approaches
did not work properly or were far too complicated.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:11>
Old description:
> I reviewed that pull request and after some testing I came to the
> conclusion, that `math.isclose` with a tolerance of `1e-9` is the best
> solution to fix the floating point rounding errors. All other approaches
> did not work properly or were far too complicated.
New description:
If someone wants to use the `step` attribute as provided by the HTML field
`<input type="number" ...>` , she/he has to specify that using for
instance
`FloatField(widget=NumberInput(attrs={'step': 0.5}))`.
Since the HTML standard offers a `step` attribute on input fields of
`type="number"`,
this feature shall be reflected by Django's `FloatField` and optionally
`DecimalField`,
rather than having to parametrize the widget.
Min- and max-values are already supported by the `FloatField`, so the
step-value
would make sense here as well. It furthermore would require to revalidate
the
step-value by Django's Form validation, rather than by HTML alone.
Patch: https://github.com/django/django/pull/14162
--
Comment (by Jacob Rief):
I reviewed that pull request and after some testing I came to the
conclusion, that `math.isclose` with a tolerance of `1e-9` is the best
solution to fix the floating point rounding errors. All other approaches
did not work properly or were far too complicated.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:12>
* owner: Kapil Bansal => Jacob Rief
Comment:
Reclaiming ticket because of missing progress.
A new pull request is here: https://github.com/django/django/pull/14532
This adds the missing documentation and release note for this feature.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:13>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:14>
Comment (by Carlton Gibson):
Current PR removes the default `step="any"` which is a problem.
Comment from PR:
> This introduces a regression from
7ec2a21be15af5b2c7513482c3bcfdd1e12782ed.
>
> We need a selenium test to cover it, but if I create an `<input
type="number" />` without the `step` specified at all, I can't enter a
non-integer value, such as `0.5` without triggering the browser validation
(''Enter a valid value'').
>
> This ties in with [https://developer.mozilla.org/en-
US/docs/Web/HTML/Element/input/number#step MDN docs for step]:
>
>> The default stepping value for number inputs is 1, allowing only
integers to be entered…
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:15>
Comment (by Jacob Rief):
I re-added the attribute `step="any"` to the `FloatField` . However, I
don't know where to add a Selenium test for check this.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:16>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
Comment:
Thanks Jacob! I'll take another look.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:17>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"4b8e9492d9003ca357a4402f831112dd72efd2f8" 4b8e9492]:
{{{
#!CommitTicketReference repository=""
revision="4b8e9492d9003ca357a4402f831112dd72efd2f8"
Refs #32559 -- Added selenium test for FloatField client-side validation.
step="any" is required for non-integer values. See:
https://developer.mozilla.org/en-
US/docs/Web/HTML/Element/input/number#step
Covers behaviour added in 7ec2a21be15af5b2c7513482c3bcfdd1e12782ed.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:18>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:19>
Comment (by Jacob Rief):
> step="any" is required for non-integer values.
shall I add this? According to the Mozilla developer docs, the `step` is
not mandatory and default to `1` for number input fields, so I'm unsure
what to change here.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:20>
Comment (by Carlton Gibson):
Hey Jacob. See
[https://github.com/django/django/pull/14532#pullrequestreview-935148664
my comment on the PR for outstanding issues]. Specifically, the docs need
`versionchanged`/`versionadded` annotations, and (more) the new validator
needs tests, matching those for the other validator classes.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:21>
Comment (by Jacob Rief):
* Added `versionadded` annotations to the docs.
* Added tests for new validator `StepValueValidator`.
* Fixed all annotations from black and flake8.
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:22>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:23>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"3a82b5f655446f0ca89e3b6a92b100aa458f348f" 3a82b5f6]:
{{{
#!CommitTicketReference repository=""
revision="3a82b5f655446f0ca89e3b6a92b100aa458f348f"
Fixed #32559 -- Added 'step_size’ to numeric form fields.
Co-authored-by: Jacob Rief <jacob...@uibk.ac.at>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32559#comment:24>