#20982: forms.BooleanField field issue
---------------------------+--------------------------------------
     Reporter:  george.li  |                    Owner:  nobody
         Type:  Bug        |                   Status:  new
    Component:  Forms      |                  Version:  1.7
     Severity:  Normal     |               Resolution:
     Keywords:             |             Triage Stage:  Unreviewed
    Has patch:  1          |      Needs documentation:  0
  Needs tests:  0          |  Patch needs improvement:  0
Easy pickings:  0          |                    UI/UX:  0
---------------------------+--------------------------------------
Changes (by abhillman):

 * status:  closed => new
 * cc: aryeh.hillman@… (added)
 * type:  Uncategorized => Bug
 * version:  1.5 => 1.7
 * has_patch:  0 => 1
 * resolution:  worksforme =>


Old description:

> in django 1.4 1.5.1
>

> {{{
> from django import forms
> forms.BooleanField().clean(True)
> }}}
> this case is success
>

> But
> {{{
> forms.BooleanField().clean(False)
> }}}
>
> {{{
> forms.BooleanField().clean('false')
> }}}
>
> in this two case are
> *** ValidationError: [u'This field is required.']
>

> source code
> {{{
>  621 class BooleanField(Field):
>  622     widget = CheckboxInput
>  623
>  624     def to_python(self, value):
>  625     ¦   """Returns a Python boolean object."""
>  626     ¦   # Explicitly check for the string 'False', which is what a
> hidden field
>  627     ¦   # will submit for False. Also check for '0', since this is
> what
>  628     ¦   # RadioSelect will provide. Because bool("True") ==
> bool('1') == True,
>  629     ¦   # we don't need to handle that explicitly.
>  630     ¦   if isinstance(value, six.string_types) and value.lower() in
> ('false', '0'):
>  631     ¦   ¦   value = False
>  632     ¦   else:
>  633     ¦   ¦   value = bool(value)
>  634     ¦   value = super(BooleanField, self).to_python(value)
>  635     ¦   if not value and self.required:
>  636     ¦   ¦   raise ValidationError(self.error_messages['required'])
>  637     ¦   return value
> }}}
>
> in 630, 'false' and '0' Transform to boolean False
> but 635 then decide False is no input value

New description:

 in django 1.4 1.5.1


 {{{
 from django import forms
 forms.BooleanField().clean(True)
 }}}
 this case is success


 But
 {{{
 forms.BooleanField().clean(False)
 }}}

 {{{
 forms.BooleanField().clean('false')
 }}}

 in this two case are
 *** ValidationError: [u'This field is required.']


 source code
 {{{
  621 class BooleanField(Field):
  622     widget = CheckboxInput
  623
  624     def to_python(self, value):
  625     ¦   """Returns a Python boolean object."""
  626     ¦   # Explicitly check for the string 'False', which is what a
 hidden field
  627     ¦   # will submit for False. Also check for '0', since this is
 what
  628     ¦   # RadioSelect will provide. Because bool("True") == bool('1')
 == True,
  629     ¦   # we don't need to handle that explicitly.
  630     ¦   if isinstance(value, six.string_types) and value.lower() in
 ('false', '0'):
  631     ¦   ¦   value = False
  632     ¦   else:
  633     ¦   ¦   value = bool(value)
  634     ¦   value = super(BooleanField, self).to_python(value)
  635     ¦   if not value and self.required:
  636     ¦   ¦   raise ValidationError(self.error_messages['required'])
  637     ¦   return value
 }}}

 in 630, 'false' and '0' Transform to boolean False
 but 635 then decide False is no input value

--

--
Ticket URL: <https://code.djangoproject.com/ticket/20982#comment:2>
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/067.3122570b4c22f3a10ffd45952bcf11a8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to