#5957: unchecked BooleanField does not raise ValidationError even if it it
required
------------------------------+---------------------------------------------
Reporter:  [EMAIL PROTECTED]  |       Owner:  nobody         
  Status:  new                |   Component:  django.newforms
 Version:  SVN                |    Keywords:                 
   Stage:  Unreviewed         |   Has_patch:  1              
------------------------------+---------------------------------------------
 I'm afraid that required=True doesn't quite work on newforms'
 [http://www.djangoproject.com/documentation/newforms/#booleanfield
 BooleanField]. According to the docs, it should trigger an error in case
 the field is unchecked, but it doesn't. I suspect the reason is that
 [http://code.djangoproject.com/changeset/6563 changeset 6563] altered
 return value of CheckboxInput from None to False in unchecked cases, but
 "required" check of the BooleanField still relies on default
 "None_to_ValidationError" method of its parent class.

 == trunk rev 6678 ==
 {{{
 >>> import django.newforms as forms
 >>> class MyForm(forms.Form):
 ...   bfield = forms.BooleanField(required=True)
 ...
 >>> f = MyForm({})
 >>> f.is_valid()
 True
 }}}

 == trunk rev 6562 ==
 {{{
 >>> import django.newforms as forms
 >>> class MyForm(forms.Form):
 ...   bfield = forms.BooleanField(required=True)
 ...
 >>> f = MyForm({})
 >>> f.is_valid()
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/usr/lib/python2.4/site-packages/django/newforms/forms.py", line
 106, in is_valid
     return self.is_bound and not bool(self.errors)
   File "/usr/lib/python2.4/site-packages/django/newforms/forms.py", line
 97, in _get_errors
     self.full_clean()
   File "/usr/lib/python2.4/site-packages/django/newforms/forms.py", line
 192, in full_clean
     value = field.clean(value)
   File "/usr/lib/python2.4/site-packages/django/newforms/fields.py", line
 454, in clean
     super(BooleanField, self).clean(value)
   File "/usr/lib/python2.4/site-packages/django/newforms/fields.py", line
 93, in clean
     raise ValidationError(ugettext(u'This field is required.'))
   File "/usr/lib/python2.4/site-
 packages/django/utils/translation/__init__.py", line 62, in ugettext
     return real_ugettext(message)
   File "/usr/lib/python2.4/site-
 packages/django/utils/translation/__init__.py", line 32, in delayed_loader
     if settings.USE_I18N:
   File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line
 28, in __getattr__
     self._import_settings()
   File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line
 55, in _import_settings
     raise EnvironmentError, "Environment variable %s is undefined." %
 ENVIRONMENT_VARIABLE
 EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is
 undefined.
 }}}
 (Maybe I should have run this in django shell, but we can still see that
 ValidationError got raised.)

 == trunk rev 6563 ==
 {{{
 >>> import django.newforms as forms
 >>> class MyForm(forms.Form):
 ...   bfield = forms.BooleanField(required=True)
 ...
 >>> f = MyForm({})
 >>> f.is_valid()
 True
 }}}
 (After changeset 6563, unchecked field is valid even if required.)

 == trunk rev 6678, with patch ==
 {{{
 >>> import django.newforms as forms
 >>> class MyForm(forms.Form):
 ...   bfield = forms.BooleanField(required=True)
 ...
 >>> f = MyForm({})
 >>> f.is_valid()
 False
 }}}
 (Supplied patch seems to fix the issue.)

-- 
Ticket URL: <http://code.djangoproject.com/ticket/5957>
Django Code <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to