Spinning my wheels on custom validator

52 views
Skip to first unread message

Dave S

unread,
Jan 12, 2015, 8:45:43 PM1/12/15
to web...@googlegroups.com
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)}}



Anthony

unread,
Jan 12, 2015, 9:02:49 PM1/12/15
to web...@googlegroups.com
Has your app been compiled?

Dave S

unread,
Jan 12, 2015, 9:24:01 PM1/12/15
to web...@googlegroups.com


On Monday, January 12, 2015 at 6:02:49 PM UTC-8, Anthony wrote:
Has your app been compiled?

Not intentionally.

The only .pyc file I see around my app is __init__.pyc in the app-level directory.

/dps

 

Dave S

unread,
Jan 12, 2015, 10:26:42 PM1/12/15
to web...@googlegroups.com


On Monday, January 12, 2015 at 5:45:43 PM UTC-8, Dave S wrote:
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.

[...]
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)



 
Gah, pfooey! I shouldn't change"_name" to "_act1", should I?

/dps

Dave S

unread,
Jan 12, 2015, 10:59:38 PM1/12/15
to web...@googlegroups.com

Also, it seems I want to change the "size" of the input field, not the "width".  But I don't see 
_size=4
working for me.  How should I do this?

/dps




Dave S

unread,
Jan 13, 2015, 2:02:19 PM1/13/15
to web...@googlegroups.com
 
It looks like Boostrap is beating me here, forcing the width to 300px.   If I change the _type to number, then I get a width of 206px (still bigger than "size=4" should be), and it breaks my input because someone (browser?) assumes a number is decimal.

/dps

Reply all
Reply to author
Forward
0 new messages