Django

Code

Ticket #4960 (closed: wontfix)

Opened 3 years ago

Last modified 2 years ago

Add "strip" keyword argument to CharField constructor

Reported by: tzellman@gmail.com Assigned to: nobody
Milestone: Component: Forms
Version: SVN Keywords: CharField strip
Cc: hv@tbz-pariv.de Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

CharField_strip_rev5753.diff.txt (0.9 kB) - added by tzellman@gmail.com on 07/23/07 18:01:42.
Patch containing example addition of "strip" keyword to CharField?

Change History

07/23/07 18:01:42 changed by tzellman@gmail.com

  • attachment CharField_strip_rev5753.diff.txt added.

Patch containing example addition of "strip" keyword to CharField?

07/23/07 18:14:03 changed by tzellman@gmail.com

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

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)

07/23/07 20:26:09 changed by Simon G. <dev@simon.net.nz>

  • stage changed from Unreviewed to Design decision needed.

09/14/07 11:28:49 changed by adrian

  • status changed from new to closed.
  • resolution set to wontfix.

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

02/28/08 08:04:40 changed by guettli

  • cc set to hv@tbz-pariv.de.

Add/Change #4960 (Add "strip" keyword argument to CharField constructor)




Change Properties
Action