Django

Code

Ticket #6362 (new)

Opened 2 years ago

Last modified 1 week ago

Remove blank spaces with strip when validating the data

Reported by: marinho Assigned to: nobody
Milestone: Component: Forms
Version: SVN Keywords: blank space strip
Cc: rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net, martin@akoha.com, farhan@thebitguru.com, net147@gmail.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Would be nice if forms remove blank spaces with .strip() method when validating data[] to cleaned_data (or saving).

Attachments

6362.normalize-kwarg.diff (6.8 kB) - added by emulbreh on 07/04/08 17:53:45.
normalize kwarg for newforms.Field

Change History

01/13/08 21:54:41 changed by bp@barryp.org

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

There is a ticket #4960: 'Add "strip" keyword argument to CharField? constructor' that was closed as "wontfix - it's too specialized for inclusion in the main library.", which I personally find quite baffling since it looks like a really low-impact 4-line change, and IMHO for the vast majority of field inputs in a form - you probably don't want leading or tailing spaces.

When prompting for a city name for example, do you really want people entering "Chicago " or " Chicago" instead of just "Chicago"? That hardly seems like a specialized need.

01/22/08 17:09:35 changed by dcramer

I'm signing this petition. Every single use-case we have of a Char or Text field would use strip() as a default before validation.

01/28/08 14:43:42 changed by jamiequint

+1 here. Seems obvious that in most form cases this should be the default. I don't want users to be able to bypass form fields simply by entering spaces in the required fields instead. Isn't it more common to want stripped data than not? I imagine this probably won't be changed to be like this for backwards compatibility sake, but it does seem to make the most sense, no?

03/06/08 16:28:02 changed by akaihola

#6717 is about URL fields and leading/trailing spaces specifically.

03/11/08 15:28:04 changed by jacob

  • stage changed from Unreviewed to Design decision needed.

07/04/08 08:46:44 changed by Karen Tracey <kmtracey@gmail.com>

I agree this would be useful. Greg Fuller has raised the issue on django-users and pointed out several ways ordinary users can get tripped up and cause problems when blanks are not stripped:

http://groups.google.com/group/django-users/browse_thread/thread/ad01501c1fd9d213#

07/04/08 16:55:43 changed by emulbreh

How about a more general normalize keyword?

CharField(normalize=lambda x: x.strip()) or CharField(normalize=lambda x: x.upper())

That could also be used to accept "+2h" or "yesterday" in DateTimeFields, or expressions for numerical fields, or ..

07/04/08 17:53:45 changed by emulbreh

  • attachment 6362.normalize-kwarg.diff added.

normalize kwarg for newforms.Field

07/04/08 17:56:11 changed by emulbreh

BooleanField.__init__() looks messy.

07/07/08 08:17:49 changed by Peter of the Norse <RahmCoff@Radio1190.org>

  • cc set to rahmcoff@radio1190.org.

07/07/08 13:13:16 changed by bruno.desthuilliers@gmail.com

As far as I'm concerned, I'd even say that all (text) fields should be stripped by default, period.

08/05/08 20:16:17 changed by zhango

  • cc changed from rahmcoff@radio1190.org to rahmcoff@radio1190.org, alex@zoomget.com.

+1 on this too please. In usability tests, most of my users accidentally entered a space in EmailFields? and were baffled when the django-based system threw an error at them, since what they typed looks fine to them. This happens too often not to be addressed.

08/09/08 08:17:32 changed by nail.xx <nail.xx@gmail.com>

+1 vote. Strip should be performed by default.

08/10/08 08:33:25 changed by nail.xx <nail.xx@gmail.com>

Possible temporary solution is available at http://www.djangosnippets.org/snippets/956/. But I'm still sure strip should be done by django as default behavior.

08/10/08 09:36:25 changed by julianb

+1. Could be made opt-out, if someone really doesn't like it.

08/10/08 09:43:08 changed by mtredinnick

Will everybody please stop putting "+1" style comments. They are *totally* useless for the purposes of the ticket. Even if 100 people added such a comment, that's less than .1% of the userbase, so is entirely non-representative. The resolution will be on the merits, not some straw count.

09/04/08 20:21:40 changed by anonymous

  • has_patch set to 1.

09/12/08 14:31:32 changed by kratorius

See also #5714 that basically belongs to this.

10/17/08 06:39:19 changed by guettli

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de.

10/20/08 10:50:06 changed by hdevries

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl.

12/24/08 08:04:46 changed by dc

-1 for the normalize(). It's overkill for simple stripping. And for complicated normalization better normalize data in form.clean_FIELD() or write own field subclass with specialized clean() method.

01/08/09 10:50:09 changed by anonymous

01/08/09 10:52:18 changed by anonymous

01/29/09 09:18:18 changed by olau

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk.

I sometimes got email from visitors claiming that the user registration form didn't work as it didn't accept their email address. Took quite a while to figure out what was wrong. Today I discovered the same problem with the shopping cart.

Adding a clean_email() method to the form to strip the data doesn't work as it is run after EmailField does its check. So it's easy to do the wrong thing, and it's not straightforward to fix.

(follow-up: ↓ 25 ) 01/29/09 09:26:43 changed by marinho

In my opinion would be simple have an attribute **"keep_blank_spaces"** in **forms.CharField?**, with defaulf value **False**. Then who wants to keep the blank space just set something like this:

    field = forms.CharField(max_length=20, keep_blank_spaces=True)

I'm sure most of cases people don't want keep blank spaces at left or right of cleaned strings.

(in reply to: ↑ 24 ) 01/30/09 08:43:52 changed by julianb

Replying to marinho:

I'm sure most of cases people don't want keep blank spaces at left or right of cleaned strings.

That might be, but as everyone here was told "Even if 100 people [think so], that's less than .1% of the userbase, so is entirely non-representative. The resolution will be on the merits, not some straw count."

I'm wondering when that is happening.

01/30/09 09:37:33 changed by marinho

@julianb

When I said "most of cases" I wasn't meaning "most of us in this discussion", but "most of projects made using Django". And for exceptional cases, there would be an attribute to set it.

03/23/09 06:43:59 changed by anonymous

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net.

03/31/09 10:32:35 changed by martin

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net, martin@akoha.com.

05/12/09 18:49:20 changed by thebitguru

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net, martin@akoha.com to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net, martin@akoha.com, farhan@thebitguru.com.

I am also looking for a decision on this (ideally, yes, do it). Posting to the django developers group for a decision (http://groups.google.com/group/django-developers/t/90352cc0da78390b).

12/27/09 09:53:34 changed by hdevries

2 years after opening this ticket still no decision? Rails' ticket management is way, way better than Django's.

01/15/10 02:39:56 changed by hvdklauw

  • milestone set to 1.2.

If you call another ticket a duplicate: http://code.djangoproject.com/ticket/11907 At least make sure the original is still looked at...

02/23/10 18:00:15 changed by ubernostrum

  • milestone deleted.

1.2 is feature-frozen, moving this feature request off the milestone.

03/03/10 23:36:30 changed by net147

  • cc changed from rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net, martin@akoha.com, farhan@thebitguru.com to rahmcoff@radio1190.org, alex@zoomget.com, hv@tbz-pariv.de, bjorn@wasda.nl, olau@iola.dk, andy@andybak.net, martin@akoha.com, farhan@thebitguru.com, net147@gmail.com.

Add/Change #6362 (Remove blank spaces with strip when validating the data)




Change Properties
Action