| Vijay Ramachandran ( @ 2008-11-22 21:53:00 |
mandatory checkbox with FormEncode
If you're developing web apps with pylons - a very cool wsgi compliant framework - you probably use formencode for validating forms. Formencode is very useful, but it takes a bit of bunging around the code to understand it well. One of its really useful philosophies is to throw an error if the form is not filled as expected - say, a format mismatch, or a mandatory field missing.
I recently ran into an issue where I have a form where the submitter needs to agree to the terms of service to continue. It should have been fairly simple - all that was needed was to specifiy that the tac_agree field was not empty. But, it took a fair bit of doing. The *only* thing which worked for me was the idea that there are other mandatory fields, and I can therefore use the RequireIfPresnet chained validator. So, my code to ensure that the checkbox is checked looks like this:
Hope you found that useful! I spent a good bit of time head scratching to get this to work.
If you're developing web apps with pylons - a very cool wsgi compliant framework - you probably use formencode for validating forms. Formencode is very useful, but it takes a bit of bunging around the code to understand it well. One of its really useful philosophies is to throw an error if the form is not filled as expected - say, a format mismatch, or a mandatory field missing.
I recently ran into an issue where I have a form where the submitter needs to agree to the terms of service to continue. It should have been fairly simple - all that was needed was to specifiy that the tac_agree field was not empty. But, it took a fair bit of doing. The *only* thing which worked for me was the idea that there are other mandatory fields, and I can therefore use the RequireIfPresnet chained validator. So, my code to ensure that the checkbox is checked looks like this:
chained_validators = [validators.RequireIfPresent('tac_agree' ,
present = 'email',
messages = {'empty' : 'Please agree to continue'})]
Hope you found that useful! I spent a good bit of time head scratching to get this to work.