Re: Help: ensuring that a checkbox is checked
Ian Bicking <[email protected]> Thu, 13 Nov 2008 11:16:51 -0600
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Vijay Ramachandran wrote:
> Hello.
>
> I have a form with a checkbox (a terms of service), like this:
>
> input id="tac_agree" name="tac_agree" type="checkbox" value="1" />
>
> I would like to ensure that this box is checked in the validator schema.
> What is the correct way to do this? I've tried many things - custom
> validator which raises Invalid if the field is false,
> validators.NotEmpty, chained_validators with a dummy field - nothing
> seems to work. I am not able to propagate the Invalid exception back to
> the front end, the Schema continues as though it didn't matter if
> tac_agree was checked or not.
A little overly minimal, but you can do:
class MySchema(Schema):
tac_agree = String()
Since the checkbox will be missing from the input if it's not checked,
so the lack of an if_missing in the validator should get it to trigger.
Maybe a better way would be:
class MySchema(Schema):
tac_agree = OneOf(['1'])
--
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=/