Lucas Kauffman
Lucas Kauffman

Reputation: 6891

Form validator in web.py, help find the mistake

I seem to have a mistake in my validator. Even when I enter -1 in my form I still get my value returned instead of blaat. Perchance someone sees my mistake?

class test:
   def __init__(self):
       self.render = web.template.render('templates/')
       self.myForm = web.form.Form(
           web.form.Textbox('minutes', id='minutes'),
           validators = [form.Validator("Minutes not correct", 
                                        lambda i: i.minutes > 0)]
       )              

   def GET(self):
       return self.render.addLog(self.myForm)

   def POST(self):
        webinput = web.input()
        if self.myForm.validates():
            return webinput.date1+webinput.minutes
        else:
            return "blaat"

Upvotes: 1

Views: 694

Answers (1)

Helgi
Helgi

Reputation: 5436

i.minutes won't be converted to int automatically, and strings compare greater than integers:

>>> '-1' > 0
True

Use int(i.munites)

By the way, form-wide validators are used to compare form fields between each other, e.g. to check that the entered passwords match. To check if any given field is correct, use one-field validators:

self.myForm = web.form.Form(
    web.form.Textbox('minutes',
        web.form.Validator("Minutes not correct", lambda x: int(x) > 0)),
)

Upvotes: 2

Related Questions