#23547: BooleanField that is required and have value False always will raise
ValidationError
-----------------------------------+--------------------------------------
Reporter: Lagovas | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.6
Severity: Normal | Resolution: duplicate
Keywords: Form BooleanField | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by timgraham):
* status: new => closed
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* resolution: => duplicate
Old description:
> class BooleanField(Field):
> def to_python(self, value):
> if isinstance(value, six.string_types) and value.lower() in
> ('false', '0'):
> value = False
> else:
> value = bool(value)
> return super(BooleanField, self).to_python(value)
>
> def validate(self, value):
> if not value and self.required:
> raise ValidationError(self.error_messages['required'],
> code='required')
> In method "validate" value is "True" or "False" as python object, because
> called after to_python.
> So if value is valid ('0' or 'false') and field is required, will be
> raised ValidationError.
> Anyway value in "validate" will be False if value is not '0' or 'false',
> so method validate should be empty because always is valid.
New description:
{{{
class BooleanField(Field):
def to_python(self, value):
if isinstance(value, six.string_types) and value.lower() in
('false', '0'):
value = False
else:
value = bool(value)
return super(BooleanField, self).to_python(value)
def validate(self, value):
if not value and self.required:
raise ValidationError(self.error_messages['required'],
code='required')
}}}
In method "validate" value is "True" or "False" as python object, because
called after to_python.
So if value is valid ('0' or 'false') and field is required, will be
raised ValidationError.
Anyway value in "validate" will be False if value is not '0' or 'false',
so method validate should be empty because always is valid.
--
Comment:
I think your complaint is the same as #23130. If not, could you please
reopen with a proposed patch?
--
Ticket URL: <https://code.djangoproject.com/ticket/23547#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.be7f5e67366bf8ba0123f8b5c9960573%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.