Code

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#9402 closed (duplicate)

Whitespace validates in any field that is required

Reported by: hdevries Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: whitespace fields
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

How to reproduce:
Create a form with a CharField that has required=True .

Put whitespace into the field ( for example 4 spaces ).

Submit the form. It will validate as is_valid().

The problem lies in fields.py at line 111 ( django trunk ):

    def clean(self, value):
        """
        Validates the given value and returns its "cleaned" value as an
        appropriate Python object.

        Raises ValidationError for any errors.
        """
        if self.required and value in EMPTY_VALUES:
            raise ValidationError(self.error_messages['required'])
        return value

At line 46 is the following:

# These values, if given to to_python(), will trigger the self.required check.
EMPTY_VALUES = (None, '')

Problem is that any amount of whitespace will get through this check. I don't know how you guys think about this ( I mean, whitespace is data ;-) ), but filling in whitespace has pretty much the same effect as filling in nothing.

Proposed fix:

        if self.required and value.rstrip() in EMPTY_VALUES:

I know I should do this with a patch etc. but I don't know how to do this. I hope this is sufficient.

Thanks in advance,

Hdevries.

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by hdevries

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

if value is None .rstrip() will raise an Error. I'm not an experienced Python programmer, so my bad for that ;-).

comment:2 Changed 6 years ago by brosner

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

Duplicate of #6362.

comment:3 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

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.