Hey guys,
I looked into this a little more.
The problem seems to be that self.custom.begin and self.custom.end are computed
as soon as the form is initialized, and the hidden fields get appended to
self.custom.end only in the call to accepts.
For this same reason, I was also having problems setting a class on a form with
a custom template using code like the following:
form = crud.read(db.table, id)
form["_class"] = "some class"
The class is set after the form is created, and hence never shows up in
self.custom.begin.
I'm using a quick work around by defining the following simple classes:
# lets you add attributes after initialization to forms rendered with custom
templates
class CustomFormBeginner(DIV):
def __init__(self, form):
self.form = form
def xml(self):
(begin, end) = self.form._xml()
return "<%s %s>" % (self.form.tag, begin)
# ensures the hidden_field() are present on every form, regardless if accepts
has been called
class CustomFormEnder(DIV):
def __init__(self, form):
self.form = form
def xml(self):
return "%s</%s>" % (self.form.hidden_fields().xml(),
self.form.tag)
And then externally set the form.custom.begin and form.custom.end at creation
time like so:
form = crud.read(db.table, id)
form["_class"] = "some class"
form.custom.begin = CustomFormBeginner(form)
form.custom.end = CustomFormEnder(form)
This seems to solve the problem, and doesn't seem to break anything in the
testing I've done.
Josh Jaques
Intern
From: [email protected] [mailto:[email protected]] On Behalf Of
Josh Jaques
Sent: Tuesday, November 23, 2010 10:35 AM
To: [email protected]
Subject: [web2py] Possible BUG: Hidden fields in SQLFORM with custom render
template
Rendering SQLFORMs with a custom template, any hidden fields I create, as well
as the hidden ID field are not displayed in the form until a call to accepts.
I think this might be a bug because the same form rendered without a custom
template will have the ID and hidden fields without calling accepts.
I tested against latest release.
Thoughts?
Sample controller action:
def index():
form = SQLFORM(db.images, db.images(1),
hidden=dict(test_field="test_value"))
accepted_form = SQLFORM(db.images, db.images(1),
hidden=dict(test_field="test_value"))
accepted_form.accepts(request.vars, formname=None)
return dict(form=form, accepted_form=accepted_form)
Sample view:
<!-- RENDER WITHOUT A TEMPLATE -->
{{=form}}
<!-- RENDER WITH A TEMPLATE -->
{{=form.custom.begin}}
Image name: <div>{{=form.custom.widget.name}}</div>
Image file: <div>{{=form.custom.widget.file}}</div>
Click here to upload: {{=form.custom.submit}}
{{=form.custom.end}}
<!-- RENDER WITH TEMPLATE AFTER ACCEPTS -->
{{form=accepted_form}}
{{=form.custom.begin}}
Image name: <div>{{=form.custom.widget.name}}</div>
Image file: <div>{{=form.custom.widget.file}}</div>
Click here to upload: {{=form.custom.submit}}
{{=form.custom.end}}
Sample output:
<!-- RENDER WITHOUT A TEMPLATE -->
<form action="" enctype="multipart/form-data" method="post"><table><tr
id="images_id__row"><td class="w2p_fl"><label for="images_id"
id="images_id__label">Id: </label></td><td class="w2p_fw"><span
id="images_id">1</span></td><td class="w2p_fc"></td></tr><tr
id="images_name__row"><td class="w2p_fl"><label for="images_name"
id="images_name__label">Name: </label></td><td class="w2p_fw"><input
class="string" id="images_name" name="name" type="text" value="a" /></td><td
class="w2p_fc"></td></tr><tr id="images_file__row"><td class="w2p_fl"><label
for="images_file" id="images_file__label">File: </label></td><td
class="w2p_fw"><input class="upload" id="images_file" name="file" type="file"
/></td><td class="w2p_fc"></td></tr><tr id="submit_record__row"><td
class="w2p_fl"></td><td class="w2p_fw"><input type="submit" value="Submit"
/></td><td class="w2p_fc"></td></tr></table><div class="hidden"><input
name="test_field" type="hidden" value="test_value" /><input name="id"
type="hidden" value="1" /></div></form>
<!-- RENDER WITH A TEMPLATE -->
<form action="" enctype="multipart/form-data" method="post">
Image name: <div><input class="string" id="images_name" name="name" type="text"
value="a" /></div>
Image file: <div><input class="upload" id="images_file" name="file" type="file"
/></div>
Click here to upload: <input type="submit" value="Submit" />
</form>
<!-- RENDER WITH TEMPLATE AFTER ACCEPTS -->
<form action="" enctype="multipart/form-data" method="post">
Image name: <div><input class="string" id="images_name" name="name" type="text"
value="" /><div class="error" id="name__error">enter from 10 to 255
characters</div></div>
Image file: <div><input class="upload" id="images_file" name="file" type="file"
/></div>
Click here to upload: <input type="submit" value="Submit" />
<div class="hidden"><input name="test_field" type="hidden" value="test_value"
/><input name="id" type="hidden" value="1" /></div></form>
This communication, including any attachments, does not necessarily represent
official policy of Seccuris Inc.
Please see http://www.seccuris.com/Contact-PrivacyPolicy.htm for further
details about Seccuris Inc.'s Privacy Policy.
If you have received this communication in error, please notify Seccuris Inc.
at [email protected]<mailto:[email protected]> or at 1-866-644-8442.
This communication, including any attachments, does not necessarily represent
official policy of Seccuris Inc.
Please see http://www.seccuris.com/Contact-PrivacyPolicy.htm for further
details about Seccuris Inc.'s Privacy Policy.
If you have received this communication in error, please notify Seccuris Inc.
at [email protected] or at 1-866-644-8442.