If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Given this form:

form = SQLFORM.factory(
                        Field('name'),
                        Field('created','date'),
                        Field('owner'),
                        Field('cat',requires=IS_IN_SET(range(0,6))))

This will produce these form elements:

<input type="text" value="" name="name" id="no_table_name" class="string"/>
<input type="text" value="" name="created" id="no_table_created" class="date"/>
<input type="text" value="" name="owner" id="no_table_owner" class="string"/>
<select name="cat" id="no_table_cat" class="string">
   <option value="0">0</option>
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
</select>
<input type="submit" value="Submit"/>

You can grab any of them using the element and elements function. Both of these functions accept any combination of tag names and attributes.

For example, this will grab the text input produced for the name Field.

form.element('input',_name='name')

This will grab all of the text inputs:

form.elements('input',_type='text')

Let's add a secondary class to all elements with a string class:

strings = form.elements(_class='string')
for s in strings:
    s['_class'] = 'string myclass'

Let's add a confirmation to the submit button:

submit = form.element("input",_type="submit")
submit["_onclick"] = "return confirm('Are you sure?');"

That's it!

Related slices

Comments (3)


Hosting graciously provided by:
Python Anywhere