Opened 17 years ago

Closed 9 years ago

Last modified 9 years ago

#4960 closed New feature (fixed)

Add "strip" keyword argument to CharField constructor

Reported by: tzellman@… Owned by: Daniel Ward
Component: Forms Version: dev
Severity: Normal Keywords: CharField strip
Cc: hv@…, marti@… Triage Stage: Accepted
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 (3)

CharField_strip_rev5753.diff.txt (888 bytes ) - added by tzellman@… 17 years ago.
Patch containing example addition of "strip" keyword to CharField
after.patch (1.3 KB ) - added by Daniel Ward 9 years ago.
Follow-up tests
potential-solution.patch (1.1 KB ) - added by Daniel Ward 9 years ago.
Potential solution by adding in 'strip' option to CharField

Download all attachments as: .zip

Change History (20)

by tzellman@…, 17 years ago

Patch containing example addition of "strip" keyword to CharField

comment:1 by tzellman@…, 17 years ago

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 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by Adrian Holovaty, 17 years ago

Resolution: wontfix
Status: newclosed

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

comment:4 by Thomas Güttler, 16 years ago

Cc: hv@… added

comment:5 by Marti Raudsepp, 11 years ago

Cc: marti@… added
Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

comment:6 by Tim Graham, 9 years ago

Patch needs improvement: set
Triage Stage: Design decision neededAccepted
Type: UncategorizedNew feature

comment:7 by Tim Graham, 9 years ago

Resolution: wontfix
Status: closednew

comment:8 by Tim Graham, 9 years ago

Patch needs improvement: unset

comment:9 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:10 by Daniel Ward, 9 years ago

Owner: changed from nobody to Daniel Ward
Patch needs improvement: unset
Status: newassigned

Uploading patches with potential solution and follow-up tests. Public documentation patches can be produced if solution accepted.

by Daniel Ward, 9 years ago

Attachment: after.patch added

Follow-up tests

by Daniel Ward, 9 years ago

Attachment: potential-solution.patch added

Potential solution by adding in 'strip' option to CharField

comment:11 by Tim Graham, 9 years ago

Patch needs improvement: set

I guess you missed the more extensive pull request that was already proposed. It still needs some improvements.

comment:12 by Tim Graham, 9 years ago

Patch needs improvement: unset

comment:13 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 11cac1b:

Fixed #4960 -- Added "strip" option to CharField

comment:14 by Thomas Güttler, 9 years ago

Thank you very much. I will use the "strip" option in most of my fields.

comment:15 by Tim Graham <timograham@…>, 9 years ago

In e7c6a2cf:

Refs #4960 -- Fixed selenium test failures for CharField strip changes.

comment:16 by Chris Lamb, 9 years ago

Should the default really be "True"? Somewhat backward incompatibile / unexpected change.

comment:17 by Tim Graham, 9 years ago

That was the conclusion of the django-developers thread linked in comment 6. Feel free to raise the issue there if you disagree.

Note: See TracTickets for help on using tickets.
Back to Top