On Thu, Feb 19, 2009 at 11:56 AM, Mark Jones <mark0...@gmail.com> wrote:

>
> forms.IntegerField(widget=forms.CheckboxInput(attrs={'value':1}),
> required=False)
>
> won't work because
>
>    def value_from_datadict(self, data, files, name):
>        if name not in data:
>            # A missing value means False because HTML form submission
> does not
>            # send results for unselected checkboxes.
>            return False
>        return super(CheckboxInput, self).value_from_datadict(data,
> files, name)
>
> returns False when the box is unchecked, and False is not one of the
> EMPTY_FIELDS so you end up with required=True even when you specify
> required=False
>

Huh?  The field doesn't become required.  Rather the IntegerField is unable
to validate the value returned in the case where the check box is unchecked:

Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:40)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django import forms
>>> class OBF(forms.Form):
...    x = forms.IntegerField(widget=forms.CheckboxInput(attrs={'value':1}),
required=False)
...
>>> f = OBF({})
>>> f.is_valid()
False
>>> f.errors
{'x': [u'Enter a whole number.']}

The error is not that the field is required, but that the value returned by
the widget cannot be interpreted by the IntegerField.


> Not sure how to fix this for real, I've created a CheckboxInteger that
> returns None instead of False....
>
> Any better ideas?
>

Don't try to marry a widget that returns one sort of data to a field that is
expecting something else?  Seriously, why are you trying to hammer in your
nail with this screwdriver?

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to