I've been following the discussions about form handling and widgets
lately and I think it would be fantastic if we get this aspect of
TurboGears working right. At present it is very awkward to handle forms
and widgets are difficult to use and extend. I also think if we get
this aspect of TG right we will be well on the way to a Rails-and
-Django beating framework (feature for feature).
Could people summarize here what they think would be needed to get TG
forms and widgets up to scratch ?
Here's my Christmas list:
1. An error_handler argument to @turbogears.expose. This would simplify
code like this:
@turbogears.expose(html="templates.form")
def add(self):
return dict(form=article_form())
@turbogears.expose(inputform=article_form())
def create(self, title, maintext, categoryID, **kw):
if cherrypy.request.form_errors:return self.add
# rest of function:add an article and redirect
to this:
@turbogears.expose(html="templates.form")
def add(self):
return dict(form=article_form())
@turbogears.expose(inputform=article_form(), error_handler=add)
# go straight to adding an article and redirect, no need to check
for errors
2. Functions to access cherrypy.request.form_errors and input_values to
save on typing, for example:
errors = turbogears.get_errors()
turbogears.set_defaults(article.toDict())
3. An "attributes" argument for widgets, for example:
TextField("name", default="Ok", attrs={'size':50, 'maxlength'100'})
-> also for compound widgets if needed:
f = TableForm(attrs={'enctype':'multipart/form-data'},
widget_attrs={'name', {'size':50}})
4. A "schema" argument for Form widgets, so you can pass in a
FormEncode schema
5. Multiple submit buttons for Forms
6. Select and radio/checkbox group widgets which select/check values
automatically
7. Select and radio/checkbox group widgets populated automatically by
passing in a SQLObject SelectResult
8. Extended SQLObject class which populates results from form request
values. When you define a subclass you provide a __public__ list like
so:
class Article(TG_SQLObject):
__public__=["title", "maintext", "categoryID"]
title = StringCol(length=200)
....other fields
In your controller:
@turbogears.expose(..)
def create(self, **kw):
Article.autoCreate(author=identity.user)
@turbogears.expose(..)
def update(self, id, **kw):
a = Article.get(id)
a.autoUpdate(updated=datetime.now())
In both cases, the attributes "title", "maintext" and "categoryID" are
automatically set if found in the cherrypy.request.paramMap, and
additional fields are then passed in as arguments. This is **sort of**
like Add/UpdateManipulators in Django.
9. A PSP and world peace :-)