Is there anyway to have multiple forms on a page without explicitly defining them in the controller?

16 views
Skip to first unread message

jlegler

unread,
Jan 26, 2009, 8:52:52 PM1/26/09
to web2py Web Framework
The reason I am asking is because I want to generate a bunch of forms
based on data in the database and have them all show up on the screen
at one time and be updateable. I can get all of the forms to show up
on the screen and I can fill them out; however, they all make the
first one submit. Is there a way make each one submit correctly
without defining all of the forms in the controller?

The way I am doing it now is defining several forms in html for
several rows in the database:

<h4>Modify Existing Function Tests</h4>
{{for i in db(db.post.test_case==request.args[0]).select():}}
<form id="myform{{=i.id}}" name="myform{{=i.id}}">
<textarea name="functionality" id="functionality"
class="functionality" >{{=i.functionality}}</textarea>
<textarea name="task" id="task" class="task" >{{=i.task}}</
textarea>
<textarea name="expected_result" id="expected_result"
class="expected_result">{{=i.expected_result}}</textarea>
{{=SELECT(test_case_status, value=i.test_status,
_name='test_status', _id='test_status', _class='test_status')}}
<textarea name="regression" id="regression" class="regression">
{{=i.regression}}</textarea>
<textarea name="comments" id="comments" class="comments">
{{=i.comments}}</textarea>
{{=INPUT(_type='checkbox', _name='affects_clients',
_id='affects_clients', _class='affects_clients',
_value=i.affects_clients)}}
{{=INPUT(_type='checkbox', _name='client_acceptance_test',
_id='client_acceptance_test', _class='client_acceptance_test',
_value=i.client_acceptance_test)}}
<input name="_formname" type="hidden" value="post{{=i.id}}" />
<input type="hidden" value="{{=i.id}}" name="id" id="id" />
<input type="hidden" value="{{=request.args[0]}}" name="test_case"
id="test_case" />
<input type="submit"/>
</form>
<div id="target">{{=i.id}}</div>

<script>
$('#myform{{=i.id}}').submit(function() {
ajax('{{=URL(r=request,f='new_post')}}',['id', 'test_case',
'functionality', 'task', 'expected_result', 'test_status',
'regression', 'comments', 'affects_clients',
'client_acceptance_test'],'target');
return false;
});
</script>
{{pass}}

I'm using the ajax function to submit the form to a different
controller which looks like this:
def new_post():
record=db(db.post.id==request.vars.id).select()
form = SQLFORM(db.post, record[0])
if form.accepts(request.vars,formname=None):
return '%s updated' % request.vars.id
elif form.errors:
return TABLE(*[TR(k,v) for k,v in form.errors.items()])

Does anyone have any ideas on how I can do this?

mdipierro

unread,
Jan 26, 2009, 11:36:32 PM1/26/09
to web2py Web Framework
didn't you just do it? What is the problem. The code is hard to read.

Massimo

jlegler

unread,
Jan 27, 2009, 1:21:11 AM1/27/09
to web2py Web Framework
Yea, the formatting gets pretty tore up on here when I post for some
reason and I haven't been doing it for very long so my code is pretty
ugly to start with. The code you are looking at will create multiple
forms; however, request.vars is the same no matter which one I
submit. It always submits the data on the top most form. I can't
figure out why for the life of me. Any ideas? I am sorry to waste
your time on this.

-jason

mdipierro

unread,
Jan 27, 2009, 1:28:00 AM1/27/09
to web2py Web Framework
because

<div id="target">{{=i.id}}</div>

<script>
$('#myform{{=i.id}}').submit(function() {
ajax('{{=URL(r=request,f='new_post')}}',['id', 'test_case',
'functionality', 'task', 'expected_result', 'test_status',
'regression', 'comments', 'affects_clients',
'client_acceptance_test'],'target');

should be


<div id="target{{=i.id}}"></div>

<script>
$('#myform{{=i.id}}').submit(function() {
ajax('{{=URL(r=request,f='new_post')}}',['id', 'test_case',
'functionality', 'task', 'expected_result', 'test_status',
'regression', 'comments', 'affects_clients',
'client_acceptance_test'],'target{{=i.id}}');

Massimo

jlegler

unread,
Jan 27, 2009, 12:00:44 PM1/27/09
to web2py Web Framework
I tried that last night which fixed the cosmetic issue of it always
posting the update message to the top div; however, it still only
updates the first record. At least now it says it updates the first
record in the correct div though. ;) I am fundamentally
misunderstanding how the submit button gets the data to request.vars.
No matter what I do, it keeps assigning the first forms data to
request.vars. I can't figure out how it is making the decision.

jlegler

unread,
Jan 27, 2009, 12:32:15 PM1/27/09
to web2py Web Framework
I think I may have figured out a way to do it. Let me play around
with it a bit and I will get back to you. Thanks for all your help.

-Jason

jlegler

unread,
Jan 27, 2009, 1:32:23 PM1/27/09
to web2py Web Framework
Yea, I got it. What I did was set up the form to generate unique ids
for every form element being rendered and then in the controller class
I take the number off of variable names so that the SQLFORM doesn't
complain about the form being modified. I am sure there is a more
elegant way to do it; but for now it works. Thanks for your help
everyone. The code is posted below for anyone that is curious. I'm
sorry it looks so bad, google makes it look even worse than it
actually is. ;)

<h4>Modify Existing Function Tests</h4>
{{for i in db(db.post.test_case==request.args[0]).select():}}
<div id="target{{=i.id}}">{{=i.id}}</div>
<form id="myform{{=i.id}}" name="myform{{=i.id}}">
<textarea name="functionality" id="functionality{{=i.id}}"
class="functionality" >{{=i.functionality}}</textarea>
<textarea name="task" id="task{{=i.id}}" class="task" >{{=i.task}}</
textarea>
<textarea name="expected_result" id="expected_result{{=i.id}}"
class="expected_result">{{=i.expected_result}}</textarea>
{{=SELECT(test_case_status, value=i.test_status,
_name='test_status', _id='test_status%s' % i.id,
_class='test_status')}}
<textarea name="regression" id="regression{{=i.id}}"
class="regression">{{=i.regression}}</textarea>
<textarea name="comments" id="comments{{=i.id}}" class="comments">
{{=i.comments}}</textarea>
{{=INPUT(_type='checkbox', _name='affects_clients',
_id='affects_clients%s' % i.id, value=i.affects_clients)}}
{{=INPUT(_type='checkbox', _name='client_acceptance_test',
_id='client_acceptance_test%s' % i.id,
value=i.client_acceptance_test)}}
<input name="_formname" type="hidden" value="post{{=i.id}}" />
<input type="hidden" value="{{=i.id}}" name="id" id="id{{=i.id}}" /
>
<input type="hidden" value="{{=request.args[0]}}" name="test_case"
id="test_case{{=i.id}}" />
<input type="submit"/>
</form>

<script>
$('#myform{{=i.id}}').submit(function() {
ajax('{{=URL(r=request,f='new_post',args=[i.id])}}',['id
{{=i.id}}', 'test_case{{=i.id}}', 'functionality{{=i.id}}', 'task
{{=i.id}}', 'expected_result{{=i.id}}', 'test_status{{=i.id}}',
'regression{{=i.id}}', 'comments{{=i.id}}', 'affects_clients
{{=i.id}}', 'client_acceptance_test{{=i.id}}'],'target{{=i.id}}');
return false;
});
</script>
{{pass}}

@auth.requires_login()
def new_post():
request.vars.id = eval('request.vars.id%s' % request.args[0])
request.vars.task = eval('request.vars.task%s' % request.args[0])
request.vars.comments = eval('request.vars.comments%s' %
request.args[0])
request.vars.functionality = eval('request.vars.functionality%s' %
request.args[0])
request.vars.client_acceptance_test = eval
('request.vars.client_acceptance_test%s' % request.args[0])
request.vars.test_status = eval('request.vars.test_status%s' %
request.args[0])
request.vars.expected_result = eval('request.vars.expected_result
%s' % request.args[0])
request.vars.affects_clients = eval('request.vars.affects_clients
%s' % request.args[0])
request.vars.regression = eval('request.vars.regression%s' %
request.args[0])
request.vars.test_case = eval('request.vars.test_case%s' %
request.args[0])

print request.vars

form = SQLFORM(db.post, db(db.post.id==request.vars.id).select()
[0])
if form.accepts(request.vars,formname=None):
return '%s updated' % request.vars.id
elif form.errors:
return TABLE(*[TR(k,v) for k,v in form.errors.items()])


Reply all
Reply to author
Forward
0 new messages