Source

pyptug / webpy / forms / fo1.py

Full commit
import web
from web import form


class Register:
    def __init__(self):
        self.render = web.template.render('templates')

        vpass = form.regexp(r".{3,20}$",
                            "must be between 3 and 20 characters")
        vemail = form.regexp(r"[^@]+@[^@]+\.[^@]+",
                             "must be a valid email address")
        self.register_form = form.Form(
            form.Textbox("username", description="Username"),
            form.Textbox("email", vemail, description="E-Mail"),
            form.Password("password", vpass, description="Password"),
            form.Password("password2", description="Repeat password"),
            form.Button("submit", type="submit", description="Register"),
            validators=[
                form.Validator("Passwords did't match",
                               lambda i: i.password == i.password2)
            ]
        )

    def GET(self):
        # do $:f.render() in the template
        f = self.register_form()
        return self.render.register(f)

    def POST(self):
        f = self.register_form()
        if not f.validates():
            return self.render.register(f)
        else:
            return "validated"

URLS = (
    '/register', Register,
)

if __name__ == "__main__":

    app = web.application(URLS, globals())
    app.run()