#4960 closed New feature (fixed)
Add "strip" keyword argument to CharField constructor
Reported by: | 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)
Change History (20)
by , 17 years ago
Attachment: | CharField_strip_rev5753.diff.txt added |
---|
comment:1 by , 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 , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 17 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Marking as wontfix -- it's too specialized for inclusion in the main library. A bunch of us in #django-sprint agreed.
comment:4 by , 17 years ago
Cc: | added |
---|
comment:5 by , 12 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
comment:6 by , 10 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Design decision needed → Accepted |
Type: | Uncategorized → New feature |
comment:7 by , 10 years ago
Resolution: | wontfix |
---|---|
Status: | closed → new |
comment:8 by , 9 years ago
Patch needs improvement: | unset |
---|
comment:9 by , 9 years ago
Patch needs improvement: | set |
---|
comment:10 by , 9 years ago
Owner: | changed from | to
---|---|
Patch needs improvement: | unset |
Status: | new → assigned |
Uploading patches with potential solution and follow-up tests. Public documentation patches can be produced if solution accepted.
by , 9 years ago
Attachment: | potential-solution.patch added |
---|
Potential solution by adding in 'strip' option to CharField
comment:11 by , 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 , 9 years ago
Patch needs improvement: | unset |
---|
comment:14 by , 9 years ago
Thank you very much. I will use the "strip" option in most of my fields.
comment:16 by , 9 years ago
Should the default really be "True"? Somewhat backward incompatibile / unexpected change.
comment:17 by , 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.
Patch containing example addition of "strip" keyword to CharField