[Django] #22739: CharField for Forms raises on empty string

5 views
Skip to first unread message

Django

unread,
May 30, 2014, 4:08:21 PM5/30/14
to django-...@googlegroups.com
#22739: CharField for Forms raises on empty string
----------------------------+--------------------
Reporter: jeremyt | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
forms.CharField raises a ValidationError when the min_length is zero and
it receives an empty string. Receiving an empty string is not the same as
the argument not being present in the arguments.

In cases,
PUT requests on a REST resource. Not sending a CharField means, do
nothing to that field but sending a blank string is an attempt to set a
field to an empty string.

In a web form, I may want to require a field be present in the POST data
as it is a part of my form but it may be blank/empty. If that field is not
present, this means that the data isn't returning the fields in which I
have set as required.


{{{
>>> from django import forms
>>> forms.CharField(min_length=0).clean("")
django.core.exceptions.ValidationError: [u'This field is required.']
}}}

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

Django

unread,
May 31, 2014, 3:50:34 AM5/31/14
to django-...@googlegroups.com
#22739: CharField for Forms raises on empty string
-------------------------+--------------------------------------
Reporter: jeremyt | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.6
Severity: Normal | Resolution: wontfix
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 erikr):

* status: new => closed
* needs_docs: => 0
* resolution: => wontfix
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

I can see your point, but the required check has been specifically written
to fail on an empty string as if it had been missing. Each field defines a
set of `empty_values`, with the default
[https://github.com/django/django/blob/master/django/core/validators.py#L15|
including an empty string]. If the value is in `empty_values`, this
[https://github.com/django/django/blob/master/django/forms/fields.py#L129|
triggers the required error].

I don't really see a way to change this behaviour in Django without
causing numerous other issues. However, a simple workaround would probably
be for you to make your own subclass of `forms.CharField`, in which you
override the `empty_values` attribute to only include `None`. You can then
use that to only get the `ValidationError` when the value was absent
instead of empty. This is not a public API as far as I can find, so it
might change in the future.

Closing as wontfix, but please reopen if you disagree.

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

Reply all
Reply to author
Forward
0 new messages