SQLFORM.grid and custom forms behavior

82 views
Skip to first unread message

Tiago Barrionuevo

unread,
Sep 26, 2017, 9:00:27 PM9/26/17
to web2py-users
Hi!
I'm trying to use custom forms for the actions used internally by the SQLFORM.grid and that are called by the grid links.
I got all that working but now I need to do my own data manipulation after the grid processes and accepts the form. However if I call "mygrid.update_form.accepted" nothing happens and it's because the grid's internal do the redirect after the update_form processing!
This preclude any form post processing outside the grid.

After a lot of research I developed the follow method to work with SQLFORM.grid and custom forms:

controller action: facilities_list()

    grid = SQLFORM.grid(query, args=request.args[:2], field_id=db.facility.id, formname='grid_facilities')

    if 'edit' in request.args:
        form = grid.update_form

        if form.accepted:
            ### custom processing here ###
            session.flash = 'Facility updated!'
            redirect(URL(request.controller, 'list'))

        response.view = '%s/facility_edit.%s' % (request.controller, request.extension)
        return dict(item_name=db.facility._singular, form=form)

    return dict(item_name=db.facility._singular, grid=grid)



However the code inside the block "if grid.update_form.accepted:" is never executed because what I explain above.

The only solution a found was change the SQLFORM.grid code to accepts one more parameter "custom_form=False" (defaults to false) and in the case it's True (when using custom forms) it don't redirects to the referrer and return normally and so I can test the updated_form.accepted, do my custom processing and redirect by myself.

Another important behavior would be make available the referrer in the grid for external access like this:

        if grid.update_form.accepted:
            ### custom processing here ###
            session.flash = 'Facility updated!'
            redirect(grid.referrer)

I apologize if already there is a possible solution for this!
Thanks in advance.

Dave S

unread,
Sep 26, 2017, 9:30:22 PM9/26/17
to web2py-users


On Tuesday, September 26, 2017 at 6:00:27 PM UTC-7, Tiago Barrionuevo wrote:
Hi!
I'm trying to use custom forms for the actions used internally by the SQLFORM.grid and that are called by the grid links.
I got all that working but now I need to do my own data manipulation after the grid processes and accepts the form. However if I call "mygrid.update_form.accepted" nothing happens and it's because the grid's internal do the redirect after the update_form processing!
This preclude any form post processing outside the grid.


Wouldn't you use the onvalidation() method?  Look at
<URL: http://web2py.com/books/default/chapter/29/07/forms-and-validators#SQLFORM-grid-signature>
and there's an example just a short scroll down.

/dps

 

Tiago Barrionuevo

unread,
Sep 26, 2017, 10:32:51 PM9/26/17
to web2py-users
I'll take a look... I think it should work.
Thanks for the reply.

Tiago Barrionuevo

unread,
Sep 27, 2017, 10:18:39 AM9/27/17
to web2py-users
I used the onvalidation() callback and it works like a charm.
And as a bonus I can invalidate the submit if my custom processing fails!
It's about a little more than a month i'm using Web2py and I still have to get its "way of thinking" to implement some features.

Thanks Dave S!


Em terça-feira, 26 de setembro de 2017 22:30:22 UTC-3, Dave S escreveu:
Reply all
Reply to author
Forward
0 new messages