http://trac.turbogears.org/turbogears/ticket/114

I haven't talked about it much, but TurboGears supports class-style
form declaration. Most of the form examples you see people giving look
like this:

form = TableForm(widgets=[
    TextField("name"),
    TextField("age", validators=Int())
])

You can also, using the code in svn now, do this:

class MyForm(TableForm):
    name = TextField()
    age = TextField(validators=Int())

form = MyForm()

It looks prettier, to be sure, and it may be possible to eliminate the
need to instantiate one of these style forms. The code to make this
work is definitely much less pleasant than the other style of code.

The problem that arises is that certain field names will be
problematic because they'll collide with names of methods and
attributes on TableForm, Form and Widget classes. (The "name"
attribute is an example of that!) Though you probably wouldn't have a
field on your form called "insert", if yuo did that would cause
trouble.

We could prefix those things with tg_, but that's ugly.

We could make a "widgetmeta" class that holds the widget metadata.
That solves part of the problem, but doesn't answer the question of
where to put the methods for the widget. Alternatively, we could put a
"fields" class to hold just your set of widgets:

class MyForm(TableForm):
    class fields:
        name = TextField()
        age = TextField(validators=Int())

The nice thing about this approach is that the fields are cleanly
namespaced *and* your form object can comfortably have whatever
methods it needs to have on it.

My preference actually remains the original form of this, where you
instantiate the form, passing in a list of widgets. It reduces a lot
of complexity (like the need for an inner class, for example).

I took the class-style declaration patch because I recognize that this
was popular with many people back in November or whenever this
originally came up. We do need to decide on this, and I think my
preference is for a "fields" or "widgets" inner class to hold the
widgets apart from the form's attributes.

Other opinions?

Kevin

--
Kevin Dangoor
Author of the Zesty News RSS newsreader

email: [EMAIL PROTECTED]
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com

Reply via email to