_before_* with SQLFORM.grid

59 views
Skip to first unread message

Paolo Valleri

unread,
Aug 18, 2014, 11:25:32 AM8/18/14
to web...@googlegroups.com
Hi all, 
a common problem is a constraint on multiple fields. There are few ways to handle this situation, I tried by using the _before_* callbacks.
Currently, if the the callback return True (which means that the constrains validation is failed) the sqlform.grid doesn't raise any message or notification.
Is that the correct behavior?

An example is that:
def index():
    def some_logic(f): return True
    db.define_table('station',
        Field('name'),
    )
    db.station._before_insert.append(some_logic)
    grid = SQLFORM.grid(db.station)

    return {'grid':grid}
By using this example, a record will be never stored but the grid will 'correctly' return every time. If you have many items, it will be difficult to understand what really happened.
I know that I can add session.flash = 'error message' in the some_logic function to display a flash message in case of error but even in this case the user will lose the form with the filled fields.

From my point of view, a possible alternative scenario would be to 'stay' in the form and to show to the user a general error message on top of the form.
Any idea?

Paolo

Anthony

unread,
Aug 18, 2014, 11:35:59 AM8/18/14
to web...@googlegroups.com
So, you want to add some logic to SQLFORM.accepts so if an insert or update fails after validation passes, the form still reports an error? Would you specify the error message in the call to SQLFORM.accepts?

Anthony

Paolo Valleri

unread,
Aug 18, 2014, 11:55:43 AM8/18/14
to web...@googlegroups.com
The first issue is that, I don't know a simple way (using sqlform.grid) to return to the form even if the validation has passed successfully. The second is to display the error message.
Maybe we can add an error_message parameter to _before_* ?


 Paolo


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/BB32aGzGtuQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Aug 18, 2014, 3:19:38 PM8/18/14
to web...@googlegroups.com
well, there's a reason why those are called "database callbacks". Those are the lower-side of things when you need to put a check somewhere.
They have nothing to do with forms or grids, and IMHO, they should stay as they are until a proper "resolution" is found for the common issue of multiple pkeys, unique constraints, and so on....

Anthony

unread,
Aug 18, 2014, 4:36:01 PM8/18/14
to web...@googlegroups.com
On Monday, August 18, 2014 11:55:43 AM UTC-4, Paolo Valleri wrote:
The first issue is that, I don't know a simple way (using sqlform.grid) to return to the form even if the validation has passed successfully.

Maybe something like:

SQLFORM.grid(..., oncreate=lambda form: redirect(URL(args=request.args, vars=request.get_vars,
                                                     user_signature
=True)))

The second is to display the error message.
Maybe we can add an error_message parameter to _before_* ?

The database callbacks are independent of forms, so form error messages probably don't belong there. Instead, another option would be to have SQLFORM.accepts check whether the insert/update actually succeeded, and if not, act as if validation failed (i.e., return False and set form.accepted to False). We'd still have to figure out how to specify and display an appropriate error message in that case.

 Anthony

Paolo Valleri

unread,
Aug 19, 2014, 3:13:58 AM8/19/14
to web...@googlegroups.com
I agree with Anthony's proposal, in a form these callbacks should be handled as validations are handled. By doing that, the user will return to the same form he/she filled. 
For a simple error message response.flash can be used. For more "in-form" messages, from my point of view, we should find a simple way to place the error_message just 'ones', otherwise we have to add a parameter in all different forms (grid, smartgrid, sqlform etc).


 Paolo

--
Reply all
Reply to author
Forward
0 new messages