On Sunday, June 13, 2010 17:25:01 Michel Albert wrote:
> Hi,
> 
> I've a small problem with TG2 and formencode. I have a set of three
> checkboxes. These three are fairly static (i.e. no items will be added
> to that list). So I don't see the point of creating a form-widget, and
> rather hard-code it into HTML. But how should I name the form elements
> so that formencode handles them properly? Additionally: which
> validator should I use for a checkbox list? validators.Set? Or is
> there something else?
> 
> I've seen in the docs, that fields which can take multiple values
> should be named "item-1", "item-2", ... , "item-n". I tried this like
> the following, but it didn't work:
> 
> <input type="checkbox" name="foo-1" value="23" />
> <input type="checkbox" name="foo-2" value="42" />
> <input type="checkbox" name="foo-3" value="64" />
> 
> I'd like this to feed the following controller-method:
> 
> @validate( validators={ "foo": validators.Set() } )
> def my_foos( self, foo=None ):
>    pass
> 
> Or something along those lines.

On a related note: I just yesterday stumbled over a feature in tw.forms that 
isn't apparent and which involves this problem:

When using a form instead of a self-created schema (the third option besides 
passing a form or a dict as you do), one needs to set the pre_validators of 
the schema to this:



from tw.forms.core import VariableDecoder
    
class ProceedToCheckoutSchema(Schema):

    pre_validators = [VariableDecoder()]
    item_options = ForEach(OptionsSchema(), convert_to_list=True)
    


The VariableDecoder validator is used to decode the parameters passed 
according to the naming-convention formencode introduced. 

tw.forms does this implicitly when the validator for the form is created. 
Another reason to try & go with tw.forms ;)

Just for future reference.

Diez

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en.

Reply via email to