Discussion:
[FE-discuss] Help: ensuring that a checkbox is checked
Vijay Ramachandran
2008-11-13 16:51:18 UTC
Permalink
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.

thanks for your help,
Vijay
Ian Bicking
2008-11-13 17:16:51 UTC
Permalink
Post by Vijay Ramachandran
Hello.
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 : ***@colorstudy.com : http://blog.ianbicking.org
Vijay Ramachandran
2008-11-13 17:39:10 UTC
Permalink
Sorry, got the wrong "To"

---------- Forwarded message ----------
From: Vijay Ramachandran <***@gmail.com>
Date: Thu, Nov 13, 2008 at 11:08 PM
Subject: Re: [FE-discuss] Help: ensuring that a checkbox is checked
To: Ian Bicking <***@colorstudy.com>


Thanks.
Post by Ian Bicking
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
tac_agree = OneOf(['1'])
For whatever reason, neither of these work. I am particularly stumped that
OneOf doesn't work. I'm using FormEncode 0.7.1 with python 2.5, if that
helps to make sense of it.

Since there are other mandatory fields in the form, I'm hacking the
required-nes by using a validators.RequireIfPresent() with chained
validators. That works.

thanks,
Vijay

Loading...