I'm trying to write a custom validator to use in a form, and I'm thinking it should be simple but the brick wall is leaving dents on my forehead.
Except for getting the occasional "
pretty please fill the form" (as when reloading the page with F5),
I get "congratulations" in the flash, and neither accepted values nor rejected values, and the debug stuff doesn't show on the console.
In the samples below, I've hot-wired the form to always produce errors, except it doesn't. I seem to missing a connecting rod somewhere.
Thanks for your help.
/dps
.
in db.py:
from gluon.validators import Validator
class IS_HEXSTR(Validator):
def __init__(self, format='%c%c%c%c', error_message='must be hex string like A09F!'):
self.format = format
self.error_message = error_message
def __call__(self, value):
try:
intvalue = int(value, 16)
print "should be valid " + intvalue
return (intvalue, "BURGLE")
except:
print "invalid " + value
return (value, self.error_message)
def formatter(self, value):
return value.upper()
(The import was added after reading
<URL:
http://www.web2pyslices.com/slice/show/1586/obligate-a-field-just-in-case-another-one-has-value>)
In my controller:
def activate():
form = FORM('Activation code:',
INPUT(_act1='left', requires=[IS_NOT_EMPTY(), IS_HEXSTR()], _width=4),
INPUT(_type='submit'))
if form.process().accepted:
response.flash = 'congratulations'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'pretty please fill the form'
return dict(form=form)
and the view is :
{{extend 'layout.html'}}
<h1>Activate Enmotus License</h1>
{{=form}}
<h2>Submitted variables</h2>
{{=BEAUTIFY(request.vars)}}
<h2>Accepted variables</h2>
{{=BEAUTIFY(form.vars)}}
<h2>
Unaccepted variables
</h2>
{{=BEAUTIFY(form.errors)}}