Helper for Custom Forms, inside controller

Skip to first unread message


Jan 2, 2009, 2:37:31 PM1/2/09
to web2py Web Framework
Hi there,

I noticed there are discussions about "Helper for Custom Forms", such

However, I am trying to do the same thing in a different approach. Not
sure whether my approach is better, so I post the idea here, request
for comments and/or criticise.

It all starts when I feel writing html in view file is more trivial
and labour-intensive, than writing handy "HTTP Building Objects" in
controller. Compare these examples:

A view, partially borrowed from
<form action="" enctype="multipart/form-data" method="post">
<!-- I can't remember the above line, so I have to copy&paste it
every time -->
{{=form.hidden_fields()}} <!-- Sometimes I forgot to write this stuff
<li><label for="color_name">Name:</label>

I like to write it in a more compact and (hopefully) more intuitive

def insert():
form=FORM( # A FORM() or SQLFORM() automatically setup hidden
fields for you
field2html(, # I will explain this later
if form.accepts(...): do_something()
return dict(content=form)

This way, the will be somewhat fat, but usually I don't
need any specific view file at all. Hope you get the idea.
By the way, my approach bring another convenience although not very
significant: let's say suddenly you need to handle one more form in
your insert action, and you plan to name them "color_form" and
"match_form" respectively, so at first you need to rename all "form"
into "color_form" inside your current view file (if any), otherwise an
error ticket will show up. But if you don't use a view file at all,
then you can just freely write something like this inside your
def foo():
return dict(content=DIV(color_form, match_form))

The downside of my approach. The page layout is defined inside
controller. It violates the MVC convention. But it seems not that bad.
What do you think?

PS: The field2html( inside my example is the real
helper function I defined inside the It can automatically
generate proper html code for a SQLField. This is part of it.

def field2html(field,value=None,**kwargs):
requires=field.requires.other if isinstance
(field.requires,IS_NULL_OR) else field.requires
if isinstance(requires,IS_IN_SET):
options=[OPTION('',_value='')] # A default empty value
options+=[OPTION(l,_value=v) for v,l in requires.options()]
return SELECT(,value=value,
if isinstance(requires,IS_DATE):
return INPUT(_class='date',_type='text',,,_value=value,requires=field.requires,**kwargs)
if field.type=='double':
return INPUT(_class='double',_type='text',,,_value=value,requires=field.requires,**kwargs)

Iceberg, 2009-Jan-03, 02:12(AM), Sat

Reply all
Reply to author
0 new messages