Opened 3 years ago

Closed 3 years ago

#32622 closed Uncategorized (duplicate)

BooleanField raises ValidationErrors when passing False

Reported by: Tonye Jack Owned by: nobody
Component: Forms Version: 3.2
Severity: Normal Keywords: forms,
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The current django form BooleanField fails when passing False.

class BooleanField(Field):
    widget = CheckboxInput

    def to_python(self, value):
        """Return a Python boolean object."""
        # Explicitly check for the string 'False', which is what a hidden field
        # will submit for False. Also check for '0', since this is what
        # RadioSelect will provide. Because bool("True") == bool('1') == True,
        # we don't need to handle that explicitly.
        if isinstance(value, str) and value.lower() in ('false', '0'):
            value = False
        else:
            value = bool(value)
        return super().to_python(value)

    def validate(self, value):
        if not value and self.required:  # <--- Source of the error this should likely use `if value is not None and self.required`
            raise ValidationError(self.error_messages['required'], code='required')

Change History (3)

comment:1 by Tonye Jack, 3 years ago

Summary: BooleanField fails when passing FalseBooleanField raises ValidationErrors when passing False

comment:2 by Tonye Jack, 3 years ago

Component: UncategorizedForms

comment:3 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #23547.

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