When the form is submitted, your function first checks if the user is 
logged in -- if not, it never gets to the code that creates or processes 
the form, so of course it can't execute your onvalidation function. Also, 
'onvalidation' should be a function, not a call to a function (i.e., just 
validate_session, not validate_session()).

Also, note that adding an "_onsubmit" attribute to SQLFORM will end up 
adding an "onsubmit" attribute to the HTML <form> tag, so the value of that 
attribute should be a Javascript string to be executed on the client side, 
not a Python function. In that case, it should trigger an Ajax callback to 
check login status, and the resulting behavior will have to be handled 
client side (either flashing the message or proceeding with submitting the 
form).

Anthony

On Thursday, December 1, 2011 9:42:11 AM UTC-5, lyn2py wrote:
>
> I have tried a few methods to get this to work, but to no avail. I
> hope I can get some help with this.
>
> **********
> The long version:
> When a user is logged in and loads a page (containing a reply form at
> the bottom), he is able to type in his reply.
>
> Presuming he takes very long to type the reply and his session expires
> (logged out from inactivity), and he hits "submit", I would like to
> check if his session is still alive before form submission (or on
> validation), and notify him if his session expired (and don't submit
> the form!)
>
> **********
> Briefly:
> - Form on the page requires user to be logged in.
> - On form submit, check that user is still logged in before processing
> form.
> - If user was accidentally logged out (or session expired), flash a
> message that "your session has expired" and stay on the same page
> (don't submit form).
>
> **********
> I have tried:
> - SQLFORM(db.table, _onsubmit=validate_session())
> - form.process(onvalidation=validate_session())
>
> Both methods seemed to have been bypassed (??). My code without the
> validation methods:
> def show_form():
>   if auth.is_logged_in():
>     form = SQLFORM(db.table)
>     if form.process().accepted:
>       response.flash = 'Reply Successful'
>     elif form.errors:
>       response.flash = 'Reply has errors'
>   else:
>     form = auth.login()
> return dict(form=form)
>
>

Reply via email to