Customizing SQLFORM.grid create, edit and update forms

3,838 views
Skip to first unread message

whowhywhat

unread,
Dec 16, 2011, 2:25:03 AM12/16/11
to web...@googlegroups.com
I could not find any post/documentation on how the create, edit and update forms generated by SQLFORM.grid could be customized. I am assuming that it is similar to CRUD form customization. However I have not been able to figure it out.
For example if i wished to have a optional field exposed upon a condition (optional spouses name if person is married, as shown in some web2py examples), how could this be done in SQLFORM.grid ? 

thanks in advance :) 

p.s:
web2py is shaping up beautifully .. hats off to Massimo, Antony and others .. 
I advocate web2py to any and everyone who is into web development.. thanks a lot!

Cliff

unread,
Dec 16, 2011, 7:53:24 AM12/16/11
to web2py-users
The grid acts like a DIV().

So if form=SQLFORM.grid

Then form[0] is the shaded area with the search bar.
Likewise form[1] is the table with the data in it.

You could do form[1].append(whatever) or form[1].insert(-1, stuff).

form.process().accepted becomes form[1].process().accepted

If I recall correctly, form[1][0][1] is the table head row with the
column titles.
To confirm, you could do print form[1][0][1][0] right after the call
to grid in your controller.

whowhywhat

unread,
Dec 16, 2011, 8:38:06 AM12/16/11
to web...@googlegroups.com
Thanks a lot for the prompt reply Cliff. The tip you gave me is something i did not know about and will be handy.. However this is useful to modify the grid in itself. I need some way to modify the subsequent Create, Edit and Update pages (if grid is editable). That is if i click on the add button (to create a new entry), i want to modify the Create form page (same with the edit/update pages). I dont know if I am missing something or am completely wrong :D .. so please humor me if so..
If i am not wrong the create form and edit form are generated using CRUD .. but how do i modify those resulting form/pages ..
Thanks in advance again.. :)
 

whowhywhat

unread,
Dec 16, 2011, 9:39:46 AM12/16/11
to web...@googlegroups.com
i am trying to figure out the SQLFORM.grid from the source. The create and update forms are generated using SQLFORM and not crud.. my mistake thinking this was done by CRUD. I guess i will need to customize the form by passing form.create_form.append(whatever) .. is this the right way? .. i will try to figure it out. Please pardon me, i am just a self taught novice coder
 

Anthony

unread,
Dec 16, 2011, 9:56:34 AM12/16/11
to web...@googlegroups.com
On Friday, December 16, 2011 9:39:46 AM UTC-5, whowhywhat wrote:
i am trying to figure out the SQLFORM.grid from the source. The create and update forms are generated using SQLFORM and not crud.. my mistake thinking this was done by CRUD. I guess i will need to customize the form by passing form.create_form.append(whatever) .. is this the right way? .. i will try to figure it out. Please pardon me, i am just a self taught novice coder

Sounds like you're on the right track. I haven't tried it, but I assume you could also use the form customization method described here: http://web2py.com/book/default/chapter/07#Custom-forms (for custom layout, not adding new fields). You would refer to form.create_form.custom.widget.yourfield, etc. Note, you would need some logic in the view to determine whether the current request is for an edit form, create form, etc. (which you can figure out from request.args).

Anthony 

whowhywhat

unread,
Dec 16, 2011, 10:15:15 AM12/16/11
to web...@googlegroups.com
:) .. that sounds right.. i guess i can just add some jquery in the view with a condition to check if the view is for edit or create form. The jquery could just insert the checkbox (if married etc.).. if checked i could use jquery to un-hide the spouse name field row. 
I will also try out the custom.widget method, but this sounds a little too laborious :D (i dont want to re-build the whole form).. is there a simpler way to just insert some extra elements like http://web2py.com/book/default/chapter/07#Adding-extra-form-elements-to-SQLFORM . I tried this, but this throws up an error (I will check and get back)
Thanks a lot Anthony and Cliff .. :D ..  any other tips ;) ?

Anthony

unread,
Dec 16, 2011, 10:23:39 AM12/16/11
to web...@googlegroups.com
 is there a simpler way to just insert some extra elements like http://web2py.com/book/default/chapter/07#Adding-extra-form-elements-to-SQLFORM . I tried this, but this throws up an error (I will check and get back)

That's the way to do it. You can also use the .element() and .elements() methods to find (and manipulate) elements in the server-side DOM: http://web2py.com/book/default/chapter/05#Server-side-DOM-and-Parsing

Anthony 

whowhywhat

unread,
Dec 16, 2011, 11:03:55 AM12/16/11
to web...@googlegroups.com
wow .. it works! :) ...

i just added the following in the controller (after calling SQLFORM.grid):

#check if form is a create form
if  len(request.args)>1 and request.args[-2]=='new' and form.create_form:
    my_extra_element = TR(LABEL('Married'),INPUT(_name='married',value=False,_type='checkbox'))
    form.create_form[0].insert(-1,my_extra_element)

now i need to add some jquery in the view (again after checking if the view is for a create or edit form).. this is so cool :D ..
thanks a lot Anthony!

will post a full example later..

Cliff

unread,
Dec 16, 2011, 1:08:56 PM12/16/11
to web2py-users
Nice.

It seems I recall that form.process() does not handle inserted fields.

Just something to look out for when you get to the next stage.

Maciej S

unread,
Feb 26, 2014, 7:38:30 AM2/26/14
to web...@googlegroups.com
Hi!

I've created SQLFORM.grid like whowhywhat did. New elements have been added to the form, but there is one drawback, if I submit, all data from new fields is lost. What should I do to keep it during callbacks?

Here is  my code:
def users():  
    grid
= SQLFORM.grid(db.auth_user)
   
   
if  len(request.args)>1 and request.args[-2]=='new' and grid.create_form:
        grid
.create_form[0].insert(-1,TR( LABEL('User type:', _id='auth_user_user_type'), INPUT(_type="radio", _name="radio_user_type", _value="normal") + LABEL('Normal user', _id="auth_user_normal_user")))
        grid
.create_form[0].insert(-1,TR('', INPUT(_type="radio", _name="radio_user_type", _value="service") + LABEL('Service user', _id="auth_user_service_user")))
        grid
.create_form[0].insert(-1,TR('', INPUT(_type="radio", _name="radio_user_type", _value="producer") + LABEL('Producer user', _id="auth_user_producer_user")))

   
return grid

Maciej

Raphael Lechner

unread,
Aug 21, 2015, 2:14:44 PM8/21/15
to web2py-users
Hi,

I have the same issue. I added a checkbox to the form, but I can't get the value after submitting.

if request.args(0) == 'new':
        my_extra_element = DIV(LABEL('Create Remote SSH Key',_class="control-label col-sm-3"),INPUT(_name='create_key',_type='checkbox'), _class="form-group")
        form[1].insert(0,my_extra_element)

on_validation -> form.vars -> create_key is missing

Have you found a solution for that? 
Is there a better approach for adding a checkbox to a grid-form?

Thanks!
Raphael

kesh

unread,
Jun 23, 2017, 4:01:05 AM6/23/17
to web2py-users
hi guys sorry to intrude, i have a problem here
db.define_table('stock_status',
    Field('Date', 'datetime', default=request.now, writable=False),
    Field('SN'),
    Field('item_description'),
    Field('Quantity','integer'),
    Field('UOM'),
    Field('stockIN','integer'),
    Field('stockOUT', 'integer'),
    #Field('stock_at_hand','integer'),
    Field('assignment')
)
class MyVirtualFields(object):
    def total_price(self):
        return self.stock_status.Quantity + self.stock_status.stockIN - self.stock_status.stockOUT
db.stock_status.stock_at_hand.virtualfields.append(MyVirtualFields()) 

i would like to do stock_in_hand = Quantity+ stockIN - stockOUT

input to this table is done through an sql grid and i have searched all over google and cant findout how to have a calculated field in grid, the documentation says this wont be displayed also am getting this error
'Field' object has no attribute 'virtualfields'
please help!

Anthony

unread,
Jun 23, 2017, 4:32:52 PM6/23/17
to web2py-users
On Friday, June 23, 2017 at 4:01:05 AM UTC-4, kesh wrote:
hi guys sorry to intrude, i have a problem here
db.define_table('stock_status',
    Field('Date', 'datetime', default=request.now, writable=False),
    Field('SN'),
    Field('item_description'),
    Field('Quantity','integer'),
    Field('UOM'),
    Field('stockIN','integer'),
    Field('stockOUT', 'integer'),
    #Field('stock_at_hand','integer'),
    Field('assignment')
)
class MyVirtualFields(object):
    def total_price(self):
        return self.stock_status.Quantity + self.stock_status.stockIN - self.stock_status.stockOUT
db.stock_status.stock_at_hand.virtualfields.append(MyVirtualFields()) 

First, the above is an old-style virtual field -- you should instead use the new style. Second, that is not the correct code even for the old style -- see the documentation: http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Old-style-virtual-fields

Anthony
Reply all
Reply to author
Forward
0 new messages