Massimo,
This is kind of complicated. It's part of a generalized application
that uses a database to to define which tables can be accessed through
CRUD. The database contains the table name, some search criteria and
decorators. The controllers look more like web2py source than a
"normal" controller. Here are the three main routines with a quick
explanation:
""""managetable() is called with a tablename, assures an action,
selects a response view, calls setsubmit() to write the requested crud
call and returns the dictionary value built in thefields(). """"
def managetable():
if request.args:
tablename = request.args[0]
id = request.args[1]
if request.args[1] == "0": session.action="create"
response.view = "%s/%s.html" % (request.controller, tablename)
form = eval(setsubmit(tablename))
return thefields(form)
if session.signon[session.searchfield]: managetable = eval
(session.signon[session.searchfield]+"(managetable)")
"""setsubmit() returns the crud command based on the session variable.
"""
def setsubmit(table):
if request.vars.submit1: session.action = "create"
if request.vars.submit2: session.action = "update"
if request.vars.submit3: session.action = "read"
if request.vars.submit4: session.action = "delete"
cmd = "crud."+session.action+"(db."+table
if session.action in ['read','update','delete']:
cmd += ",id)"
else:
cmd += ")"
return cmd
""" thefields() takes the components[0], extracts the fieldnames and
builds the crud dictionary to return."""
def thefields(curform):
mycomponents = curform.components[0]
mydict = dict(form=curform)
for mycount in range(len(mycomponents)):
mylabel = str(mycomponents[mycount][0])
mylabel = mylabel[mylabel.find('="')+2:mylabel.find('" ')]
mydict[mylabel]=mycomponents[mycount][1]
return mydict
===================
Here is an abbreviated Model:
db.define_table('menu',
db.Field('linkname','string',default='link_name'),
db.Field('linkactive','string',default='False'),
db.Field('linkcontroller','string',default='index'),
db.Field('tablefield','string',default='tablename.fieldname'),
db.Field('linkcondition','string',length=64,default='True'),
db.Field
('tabledecorator','string',length=64,default='auth.requires_login()'),
db.Field('tablematch','string',length=64,default='id'))
db.define_table('town',
db.Field('townname','string'),
db.Field('towncode','string'))
##
db.define_table('location',
db.Field('poleno','string'),
db.Field('town_id',db.town))
##
db.town.townname.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB
(db,'town.townname')]
db.town.towncode.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB
(db,'town.towncode')]
db.location.poleno.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB
(db,'location.poleno')]
db.location.town_id.requires=IS_IN_DB(db,
db.town.id ,'%(townname)s')
===================
Here is a portion of a custom view using the generated fields:
{{extend 'layout.html'}}
<h1>location
{{if session.action == "update":}}
Update
{{elif session.action == "create":}}
Add
{{pass}}
</h1>
<form action="" enctype="multipart/form-data" method="post">
<table>
<tr id=location_poleno__row>
<td><label for="location_poleno" id="location_poleno__label">Poleno:
</label></td>
{{if session.action in ["update","create"]:}}
{{=location_poleno}}
{{else:}}
<td>{{=form.custom.inpval.poleno}}</td>
{{pass}}
<td></td>
</tr>
<tr id=location_town_id__row>
<td><label for="location_town_id" id="location_town_id__label">Town
Id: </label></td>
{{if session.action in ["update","create"]:}}
{{=location_town_id}}
{{else:}}
<!-- THIS IS THE LINE THAT SHOULD RETURN A SELECTED FIELD FROM A
SELECT TAG -->
<td>{{=form.custom.widget.town_id}}</td>
{{pass}}
<td></td>
</tr>
.............
See the forth line from the bottom. (Caps for emphasis, not yelling).
Two lines above that, {{=location_town_id}} produces a dropdown
<select> field that works perfectly in crud, including errors. For
reference, this all started in thread:
http://groups.google.com/group/web2py/browse_thread/thread/5ff39195cb22d810/46792d84246385dd?q=crud+validation&lnk=ol&
I'm including this reference just in case it help to have the
background.
I'd be glad to send you the entire application if that helps. Just
let me know.
Much thanks!