extra non-persistent fields in forms

131 views
Skip to first unread message

Carlos

unread,
Dec 12, 2010, 6:54:23 PM12/12/10
to web2py-users
Hi,

Is it possible to define extra non-persistent fields in forms, either
by CRUD or SQLFORM?.

Or is SQLFORM.factory the only option?.

Thanks,

Carlos

weheh

unread,
Dec 12, 2010, 10:33:02 PM12/12/10
to web2py-users
Have you looked at virtual fields and computed fields?

Carlos

unread,
Dec 12, 2010, 11:09:53 PM12/12/10
to web2py-users
Hi weheh,

Yes, I've looked at virtual and computed fields, but I believe (not
sure) they do not provide the behavior I require.

I need a non-persistent field/widget (e.g. checkboxes) in a regular
persistent SQLFORM, such that the flow after the submit will depend on
these non-persistent values. For reference having one checkbox in the
SQLFORM that says "I want to proceed to X step after submit".

I'm not sure if I'm explaining well?.

Thanks,

Carlos

Bruno Rocha

unread,
Dec 12, 2010, 11:38:28 PM12/12/10
to web...@googlegroups.com
form = SQLFORM(db.table)
myinput = INPUT(_type='text',_name='testing',_value='blablabla')
form.append(myinput)

Example in shell:

>>> a = SQLFORM(db.category)
>>> a
<gluon.sqlhtml.SQLFORM object at 0x101c42d50>
>>> print a
<form action="" enctype="multipart/form-data" method="post"><table><tr id="category_name__row"><td class="w2p_fl"><label for="category_name" id="category_name__label">Name: </label></td><td class="w2p_fw"><input class="string" id="category_name" name="name" type="text" value="" /></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></form>
>>> a.append(INPUT(_type='text',_name='testing'))
>>> a
<gluon.sqlhtml.SQLFORM object at 0x101c42d50>
>>> print a
<form action="" enctype="multipart/form-data" method="post"><table><tr id="category_name__row"><td class="w2p_fl"><label for="category_name" id="category_name__label">Name: </label></td><td class="w2p_fw"><input class="string" id="category_name" name="name" type="text" value="" /></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><input name="testing" type="text" /></form>
>>> 


2010/12/13 Carlos <carlos...@gmail.com>



--

Bruno Rocha
http://about.me/rochacbruno/bio

Bruno Rocha

unread,
Dec 12, 2010, 11:46:10 PM12/12/10
to web...@googlegroups.com
One more thing.

using form.append() the new item will be appended at the end of the form.

if you want this item to be inserted ina  desired position:

a.insert(1,INPUT(_name='itemname'))

when '1' is the position on index starting at 0, in every form created by SQLFORM the item 0 is <table>, and you can reffer to this table as:

form[0].insert() or form[0].append

look this example:

>>> a = SQLFORM(db.category)
>>> a[0]
<gluon.html.TABLE object at 0x101f839d0>
>>> a[0].insert(1,INPUT(_name='bruno'))
>>> print a
<form action="" enctype="multipart/form-data" method="post"><table><tr id="category_name__row"><td class="w2p_fl"><label for="category_name" id="category_name__label">Name: </label></td><td class="w2p_fw"><input class="string" id="category_name" name="name" type="text" value="" /></td><td class="w2p_fc"></td></tr><tr><td><input name="bruno" type="text" /></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></form>

2010/12/13 Bruno Rocha <rocha...@gmail.com>

weheh

unread,
Dec 12, 2010, 11:55:18 PM12/12/10
to web2py-users
I prefer customizing forms using SQLFORM.factory instead of the form
insert/append method. It's a matter of choice and coding style, but
both will work.

Bruno Rocha

unread,
Dec 13, 2010, 12:26:43 AM12/13/10
to web...@googlegroups.com
The is that SQLFORM.factory does not generate form based on db.tables, and SQLFORM does, if you need to generate tables based on db.table and include extra fields to this form, I guess the only options is by appending/inserting to elements to the form, or doing that at JavaScript.

2010/12/13 weheh <richard...@verizon.net>

I prefer customizing forms using SQLFORM.factory instead of the form
insert/append method. It's a matter of choice and coding style, but
both will work.

Bruno Rocha

unread,
Dec 13, 2010, 12:34:40 AM12/13/10
to web...@googlegroups.com
Sorry, my fault, SQLFORM.factory generates forms based on tables, but this does not make the inserts automatically.

if you want to use that to include extra fields in a form it is a good choice, but you'll need to implement the 'inserts' for the table.

orm=SQLFORM.factory(db.client,Field('extra_field'))
if form.accepts(request.vars):
id = db.client.insert(**db.client._filter_fields(form.vars))
form.vars.client=id
response.flash='Thanks for filling the form'
return dict(form=form)

The advantage here is that you can set validators to the extra fields.

Look this example in a shell:

>>> form = SQLFORM.factory(db.category,Field('new_field'))
>>> print form
<form action="" enctype="multipart/form-data" method="post"><table><tr id="no_table_name__row"><td class="w2p_fl"><label for="no_table_name" id="no_table_name__label">Name: </label></td><td class="w2p_fw"><input class="string" id="no_table_name" name="name" type="text" value="" /></td><td class="w2p_fc"></td></tr><tr id="no_table_new_field__row"><td class="w2p_fl"><label for="no_table_new_field" id="no_table_new_field__label">New Field: </label></td><td class="w2p_fw"><input class="string" id="no_table_new_field" name="new_field" type="text" value="" /></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></form>
>>> 

Carlos

unread,
Dec 13, 2010, 10:38:04 AM12/13/10
to web2py-users
Thanks a lot !

On Dec 12, 11:34 pm, Bruno Rocha <rochacbr...@gmail.com> wrote:
> Sorry, my fault, SQLFORM.factory generates forms based on tables, but this
> does not make the inserts automatically.
>
> if you want to use that to include extra fields in a form it is a good
> choice, but you'll need to implement the 'inserts' for the table.
>
> orm=SQLFORM <http://web2py.com/book/default/docstring/SQLFORM>.factory(db.client,Field('extra_field'))
>     if form.accepts(request
> <http://web2py.com/book/default/docstring/request>.vars):
>         id = db.client.insert(**db.client._filter_fields(form.vars))
>         form.vars.client=id
>         response
> <http://web2py.com/book/default/docstring/response>.flash='Thanks for
> filling the form'
>     return dict(form=form)
>
> The advantage here is that you can set validators to the extra fields.
>
> *Look this example in a shell:*
>
> >>> form = SQLFORM.factory(db.category,Field('new_field'))
> >>> print form
>
> <form action="" enctype="multipart/form-data" method="post"><table><tr
> id="no_table_name__row"><td class="w2p_fl"><label for="no_table_name"
> id="no_table_name__label">Name: </label></td><td class="w2p_fw"><input
> class="string" id="no_table_name" name="name" type="text" value=""
> /></td><td class="w2p_fc"></td></tr><tr id="no_table_new_field__row"><td
> class="w2p_fl"><label for="no_table_new_field"
> id="no_table_new_field__label">New Field: </label></td><td class="w2p_fw"><i
> *nput class="string" id="no_table_new_field" name="new_field" type="text"
> value="" />*</td><td class="w2p_fc"></td></tr><tr
Reply all
Reply to author
Forward
0 new messages