On Mon, Sep 21, 2009 at 11:11 AM, edgarsmolow <[email protected]> wrote:
>
> Hi Everyone:
>
> I have an HTML form that I'd like to validate using formencode, if
> possible.  The form has three main options represented by radio
> buttons.  If one of those options is selected, there are four sub-
> options to choose from, represented by checkboxes.  For example,
> suppose the three main options were football, baseball and basketball;
> and baseball has four sub-options: pitcher, catcher, infield,
> outfield).
>
> The trick here is that at least one of sub-options (checkboxes) must
> be selected if baseball is selected.  The other main options have no
> suboptions.  But, none of baseball's sub-options should be selected if
> football or basketball were selected.
>
> How can formencode validator(s) be used to set up this kind of
> validation?

First of all, don't create part-time controls with Javascript and
expect them to be sometimes in the result.  Make static controls for
everything and hide the ones that are irrelevant.  That way your
validator will always return a fixed set of variables, and the
irrelevant ones will have their default values.

If a group of controls has a dependency relationship, you'll have to
test that either in a chained validator or nested validator.  If your
HTML fields are all flat (not nested), you can do the type checking in
the main validators (integer, one of list), but allow part-time
controls to be empty (don't require them).  Then in the chained
validator, determined which sub-options are required and and raise an
error if they're empty.  You may also have to do additional work in
the chained operator depending on the constraints you wish to impose.

A nested validator is a little group of controls.  E.g., the baseball
controls, the football controls.  Each sub-validator has a dict value,
and the HTML names of the controls must follow the NestedVariables
rules.  The resulting form parameters will be a dict in a dict.  If
the sub-groups are totally unique, you can use a different validator
for each one.  If there's some commonality between them (e.g., all
have three pulldowns which differ only in their options), you may be
able to write one validator class that can be instantiated differently
for each one.

-- 
Mike Orr <[email protected]>

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

Reply via email to