Some treads here disus the handling of custom forms.
One hint was use SQLFORM in the controller and build anything else in
the view.
I don't like the idea to do things twice.
Another hint was {{=form[0][1][1][0]}}.
Not so handy and may break when fields are added to the table.
So i wrapped this in an function. Now I have both quick SQLForms for
development and all posiilites for make my one styled forms.
In a Form I can now do:
<ol>
{{=LI(form.elm("name"))}}
{{=LI(form.elm("match"))}}
</ol>
rendert as:
<li><label for="name">Name: </label><input class="string" id="name"
name="name" type="text" value="" /></li>
<li><label for="match">Match: </label><select class="reference match"
id="match" name="match"><option value="1">xyz</option></select></li>
or:
{{=form.elm("reference", _type="hidden")}}
{{=form.elm("id")}}{{=form.hidden_fields()}}
rendert as:
<input class="reference reference" id="reference" name="reference"
type="hidden" value="" />
<input name="_formkey" type="hidden" value="be9653b2-73ab-459b-b63d-
e3831198da3b" /><input name="_formname" type="hidden" value="color" />
or:
{{for elm in "cmyk":}}
{{=LI(form.elm(elm, SPAN(elm.capitalize()+": ",
_id="color_"+elm) ) )}}
{{pass}}
rendered as:
<li><label for="c"><span id="color_c">C: </span></label><input
class="integer" id="c" name="c" type="text" value="0" /></li>
<li><label for="m"><span id="color_m">M: </span></label><input
class="integer" id="m" name="m" type="text" value="0" /></li>
<li><label for="y"><span id="color_y">Y: </span></label><input
class="integer" id="y" name="y" type="text" value="0" /></li>
<li><label for="k"><span id="color_k">K: </span></label><input
class="integer" id="k" name="k" type="text" value="0" /></li>
Any suggestions are welcome.
Martin
Here's the code that I put in a model:
#patch SQLFORM
def __elm(self, elm, label=True, _id=True, **kargs):
if elm is "id":
#why is id not in form.hidden_fields()?
return self.components[1] if len(self.components) > 1 else ""
elif elm is "delete":
fid = -2
else:
fid = self.fields.index(elm)-1
if fid < 0: raise SyntaxError, "Field not in Form!"
field = self.components[0][fid][1][0]
if elm is "delete" and field.attributes["_id"] is not
"delete_record": return ""
if _id is True and "_name" in field.attributes:
#name as id, this is maybe a little to specific for only me
field.attributes["_id"] = field.attributes["_name"]
elif type(_id) is str:
#custom id
field.attributes["_id"] = _id
for key in kargs.keys():
field.attributes[key] = kargs[key]
if "_type" in field.attributes.keys() \
and field.attributes["_type"] is "hidden":
label=False
if label:
lb = self.components[0][fid][0][0]
if _id is True and "_id" in field.attributes:
lb.attributes["_for"] = field.attributes["_id"]
elif type(_id) is str:
lb.attributes["_for"] = _id
lb.attributes["_id"] = None
if label is not True:
lb.components[0] = XML(label)
field = XML(lb.xml() + field.xml())
return field
SQLFORM.elm = __elm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---