Hi everyone, I've been thinking about web.py forms, and I realize that
it could be great if you could define a form defining a class. Something
like that:
from web import form
class MyForm(form):
name = form.Textbox('name')
password = form.Password('password')
password2 = form.Password('password2')
validator1 = form.Validator("Passwords didn't match.", lambda i:
i.password == i.password2)
and then use like normal web.py form:
f = MyForm()
f.render()
...
f.validates()
...
I write a patch to make that avaliable in current web.py.
What do you think about that?
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.
>From 1cf20562b7ac7f0de3b66353377f1516bef8af8a Mon Sep 17 00:00:00 2001
From: danigm <[email protected]>
Date: Wed, 20 Oct 2010 18:38:50 +0200
Subject: [PATCH] Allow declare forms using inheritance.
---
web/form.py | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/web/form.py b/web/form.py
index 1ea60ad..826f276 100644
--- a/web/form.py
+++ b/web/form.py
@@ -22,9 +22,17 @@ class Form:
"""
def __init__(self, *inputs, **kw):
self.inputs = inputs
+ if not self.inputs:
+ self.inputs = [getattr(self, i) for i in dir(self) if\
+ hasattr(self, i) and\
+ isinstance(getattr(self, i), Input)]
self.valid = True
self.note = None
self.validators = kw.pop('validators', [])
+ if not self.validators:
+ self.validators = [getattr(self, i) for i in dir(self) if\
+ hasattr(self, i) and\
+ isinstance(getattr(self, i), Validator)]
def __call__(self, x=None):
o = copy.deepcopy(self)
--
1.7.3.1