Re: ForEach and checkboxes?

Ian Bicking <[email protected]> Thu, 28 Aug 2008 08:43:31 -0500
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Matej Barič wrote:
> Hello,
> I use ToscaWidgets but I believe this is more of a FormEncode problem:
> In my form schema I use myfield = FormFieldRepeater(widget=Checkbox,  
> repetitions=3). FormFieldRepeater uses the ForEach validator which in  
> my case returns [True] instead of [False, False, True] if only the  
> last checkbox is checked. I think the problem is already in schema.py  
> where a method simply returns validator.if_missing value which may be  
> ok for normal input fields but not checkboxes. Maybe  
> ForEach.if_missing shouldn't simply a value of [].
> Is there any workaround for what I want?

Checkboxes are tricky.  The naming trick (checkbox-1, checkbox-2, etc) 
doesn't really work that well, as variabledecode doesn't fill in empties 
(i.e., checkbox-2=on becomes ['on']).  That's probably the problem you 
are having.  It works okay if you have other fields repeating alongside 
the checkboxes (i.e., you are repeating a schema that contains a 
checkbox and something like a text field).

Outside of FormEncode I've used hidden fields to manage this, so that 
the hidden field serves as a kind of default.  There's nothing in 
FormEncode to really handle this, but you could handle it before you 
pass the fields to FormEncode, like:

for field_name, field_value in fields.items():
     if field_name.endswith('.default'):
         orig_name = field_name[:-len('.default')]
         if orig_name not in fields:
             fields[orig_name] = field_value
         del fields[field_name]

Then put in <input type="hidden" name="checkbox-1.default" value="">. 
Potentially variabledecode itself could have this functionality added.

-- 
Ian Bicking : [email protected] : http://blog.ianbicking.org

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
FormEncode-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-discuss