Oh nice! I didn't know you could use SQLFORM if you weren't using a
database, very nice. Sure looks allot nicer by default.
So based on your example I'm doing this...
def roomcreate():
form = SQLFORM.factory(
Field('room_name', requires=IS_NOT_EMPTY()),
Field('attendee_pw',requires=IS_NOT_EMPTY()),
Field('moderator_pw',requires=IS_NOT_EMPTY()),
)
In the view, {{=form}} gives a pretty <table> version of the form.
What is the proper way to loop through and get the name and
formfield? I couldn't figure out how to apply your suggestion of
form['_name']. I know I'm doing something wrong with the syntax but
this is what I have:
{{ for fieldname in form: }}
<li><label>{{=fieldname['_name']}}</label>{{=fieldname}}</li>
{{pass}}
As a end result I want something like this
<li><label>Room Name</label><input class="string" name="room_name"/></
li>
<li><label>Attendee Pw</label><input class="string" name="attendee_pw"/
></li>
<li><label>Moderator Pw</label><input class="string"
name="moderator_pw"/></li>
I saw in the documentation some form.custom stuff (http://web2py.com/
book/default/chapter/07#Custom-forms) which is nice because it allows
me to call name (form.custom.label) and form element
(form.custom.widget). I was able to figure out a way to loop through
it but I seem to be getting some things that aren't part of the form.
Here is what I'm doing now:
{{=form.custom.begin}}
<ul>
{{ for fieldname in form.custom.label: }}
<li><label>{{=form.custom.label[fieldname]}}</label>
{{=form.custom.widget[fieldname]}}</li>
{{pass}}
<li>{{=form.custom.submit}}</li>
</ul>
{{=form.custom.end}}
The above works and gives me a nice form in the format I was expecting
but I get a extra "Id" item and I have no idea where that is coming
from. So I'm wondering if I'm looping through this properly or if
there is a different way I should be doing this. Here is the
resulting HTML from the above:
<form action="" enctype="multipart/form-data" method="post">
<ul>
<li><label>Moderator Pw</label> <input class="string"
id="no_table_moderator_pw" name="moderator_pw" type="text" value="" /
></li>
<li><label>Room Name</label> <input class="string"
id="no_table_room_name" name="room_name" type="text" value="" /></li>
<li><label>Id</label> </li>
<li><label>Attendee Pw</label> <input class="string"
id="no_table_attendee_pw" name="attendee_pw" type="text" value="" /></
li>
<li><input type="submit" value="Submit" /></li>
</ul>
<div class="hidden"><input name="_formkey" type="hidden"
value="8aa047d2-0f5d-43cc-b146-74720ea09f78" /><input name="_formname"
type="hidden" value="no_table_create" /></div></form>
I'm not sure if the first or second example is the better way to go
but it's nice working through the features of web2py. Thanks again
for the help,
-Keith
On Sep 21, 5:29 pm, mdipierro <[email protected]> wrote:
> You are using low level functionality
>
> for field in form loops over the tags contained in the <form>..</form>
> which may or may not have a name. Anyway you are looking for
> form['_name'].
>
> I would recommend doing this instead:
>
> This is my controller:
>
> def roomcreate():
> form = SQLFORM.factory(
> Field('room_name', requires=IS_NOT_EMPTY()),
> Field('attendee_pw',requires=IS_NOT_EMPTY()),
> Field('moderator_pw',requires=IS_NOT_EMPTY()),
>
> Field('welcome_msg',requires=IS_NOT_EMPTY()),formstyle='ul')
>
> {{=form}}
>
> On Sep 21, 4:55 pm, Keith <[email protected]> wrote:
>
>
>
> > I'm trying to figure out how to get field name when using FORM in the
> > controller. I can draw the form input fields or put a label on it,
> > but can't seem to figure out how to just call the form field name as a
> > variable in the view. Ideally I'd like to be able to do something
> > like this in the view:
>
> > {{ for field in form: }}
> > <li> <label>{{=field.name}}</label> {{=field}} </li>
> > {{pass}}
>
> > This is my controller:
>
> > def roomcreate():
> > form = FORM(
> > INPUT(_name='room_name', requires=IS_NOT_EMPTY()),
> > INPUT(_name='attendee_pw',
> > requires=IS_NOT_EMPTY()),
> > INPUT(_name='moderator_pw',
> > requires=IS_NOT_EMPTY()),
> > INPUT(_name='welcome_msg',
> > requires=IS_NOT_EMPTY()),
> > INPUT(_type='submit')
>
> > I've tried wrapping the the INPUT inside of a "LABEL" tag but that
> > doesn't give me the end result HTML I need. I really need to be able
> > to call the field itself and field name as separate variables in the
> > view. I know worst case I can manually create the HTML for the whole
> > form but I'm trying to learn and figure out if this is possible first
> > before I do that.
>
> > Thanks for any help or pointers.
>
> > -Keith