[Django] #30125: forms.DecimalField has incorrect validator

9 views
Skip to first unread message

Django

unread,
Jan 23, 2019, 2:01:23 PM1/23/19
to django-...@googlegroups.com
#30125: forms.DecimalField has incorrect validator
--------------------------------------+------------------------
Reporter: Rom4eg | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
django-source: site-packages/django/core/validators.py:356


{{{
@deconstructible
class MinValueValidator(BaseValidator):
message = _('Ensure this value is greater than or equal to
%(limit_value)s.')
code = 'min_value'
def compare(self, a, b):
return a < b
}}}


The message says 'Ensure this value is greater than or (!!!)equal(!!!)'
but compare func make only 'Greater then' comparison.

--
Ticket URL: <https://code.djangoproject.com/ticket/30125>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 23, 2019, 2:18:22 PM1/23/19
to django-...@googlegroups.com
#30125: forms.DecimalField has incorrect validator
----------------------------------+--------------------------------------
Reporter: Roman Paranichev | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

The validator is correct. `compare()` returns `True` if the value is
invalid. `a` is the value being validated and `b` is the limit value.

--
Ticket URL: <https://code.djangoproject.com/ticket/30125#comment:1>

Django

unread,
Jan 23, 2019, 2:21:38 PM1/23/19
to django-...@googlegroups.com
#30125: forms.DecimalField has incorrect validator
----------------------------------+--------------------------------------
Reporter: Roman Paranichev | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Roman Paranichev):

Steps to reproduce:
1. create html form

{{{
<form action="/payment/">
<input type="number" step="0.01" name="amount" value="0.01" required>
<input type="submit" value="Submit">
</form>
}}}

2. create django-form for validation:

{{{
from django import forms

class CreatePaymentForm(forms.Form):
amount = forms.DecimalField(min_value=0.01, max_digits=12,
decimal_places=2)
}}}

3. Try to validate from in a view class:

{{{
import json
from django.http import HttpResponse
from django.views.generic import View
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from api.forms import CreatePaymentForm

@method_decorator(login_required, name='dispatch')
@method_decorator(csrf_exempt, name='dispatch')
class PaymentView(View):

def post(self, request):
form = CreatePaymentForm(request.POST)
if form.is_valid():
pass
print("post: %s, errors: %s" % (request.POST, form.errors))
return HttpResponse(json.dumps({'errors': form.errors}),
status=400)
}}}

The output will be:
post: <QueryDict: {'amount': ['0.02']}>, errors: <ul
class="errorlist"><li>amount<ul class="errorlist"><li>Ensure this value is
greater than or equal to 0.02.</li></ul></li></ul>

--
Ticket URL: <https://code.djangoproject.com/ticket/30125#comment:2>

Django

unread,
Jan 23, 2019, 6:05:53 PM1/23/19
to django-...@googlegroups.com
#30125: forms.DecimalField has incorrect validator
----------------------------------+--------------------------------------
Reporter: Roman Paranichev | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Tim Graham):

Please try with `min_value=Decimal('0.02')` -- floats like 0.02 may not
keep precision. A better test would use `MinValueValidator` directly
rather than require a full request/response -- see `tests/validators` in
Django's source code.

--
Ticket URL: <https://code.djangoproject.com/ticket/30125#comment:3>

Reply all
Reply to author
Forward
0 new messages