Code

Opened 7 years ago

Closed 7 years ago

Last modified 18 months ago

#4960 closed Uncategorized (wontfix)

Add "strip" keyword argument to CharField constructor

Reported by: tzellman@… Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords: CharField strip
Cc: hv@…, marti@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am suggesting that a strip keyword (or something similar) gets added to the CharField constructor in newforms/fields.py.

This would allow for certain fields to get stripped before being cleaned.
Example: Assume you have a CharField with min_length set to 3. Typing in 3 spaces will be valid, which in some cases is fine. But other times, you want to strip the whitespace. This way, a field filled with spaces will be considered to be empty, if the "strip" argument is True.

This is only a suggestion, but it would be nice to have. I know that I could easily sub-class and create my own "StrippedCharField", and that might be the right way to go, but I thought this might be a common issue for other people as well.

Thanks!

Attachments (1)

CharField_strip_rev5753.diff.txt (888 bytes) - added by tzellman@… 7 years ago.
Patch containing example addition of "strip" keyword to CharField

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by tzellman@…

Patch containing example addition of "strip" keyword to CharField

comment:1 Changed 7 years ago by tzellman@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Attached is another alternative, as I mentioned:

class StrippedCharField(forms.CharField):
    def __init__(self, max_length=None, min_length=None, strip=True, *args, **kwargs):
        super(StrippedCharField, self).__init__(max_length, min_length, *args, **kwargs)
        self.strip = strip

    def clean(self, value):
        if self.strip:
            value = value.strip()
        return super(StrippedCharField, self).clean(value)

comment:2 Changed 7 years ago by Simon G. <dev@…>

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

Marking as wontfix -- it's too specialized for inclusion in the main library. A bunch of us in #django-sprint agreed.

comment:4 Changed 6 years ago by guettli

  • Cc hv@… added

comment:5 Changed 18 months ago by intgr

  • Cc marti@… added
  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.