[web2py] question about onvalidation function

561 views
Skip to first unread message

Richard

unread,
Dec 23, 2011, 2:32:22 PM12/23/11
to web2py-users
Hello,

In the book there is this example :

db.define_table('numbers',
Field('a', 'integer'),
Field('b', 'integer'),
Field('c', 'integer', readable=False, writable=False))

def my_form_processing(form):
c = form.vars.a * form.vars.b
if c < 0:
form.errors.b = 'a*b cannot be negative'
else:
form.vars.c = c

def insert_numbers():
form = SQLFORM(db.numbers)
if form.process(onvalidation=my_form_processing).accepted:
session.flash = 'record inserted'
redirect(URL())
return dict(form=form)


Where "my_form_processing" function is called without bracket and
arguments...

I try to do this that failed :

def my_form_processing(table_name, form):
form.vars.field1 = table_name # I know useless, it just
demonstration for purpose of my question...

...

if form.process(onvalidation=my_form_processing(request.args(0),
form)).accepted:

Then I did :

def my_form_processing(form):
form.vars.field1 = request.args(0)

...

if form.process(onvalidation=my_form_processing).accepted:

That actually works...

I would like to make sure that I understand onvalidation properly...

Is "my_form_processing" just a extension of the "insert_numbers()"
function?

And if so, does it have access to all the same variables
(environnement) of "inser_numbers()"??

Thank you!

Richard


Massimo Di Pierro

unread,
Dec 23, 2011, 4:13:04 PM12/23/11
to web2py-users
onvalidation = my_form_processing

registers a callback (called after validation and before accepting).
This does not work

def my_form_processing(table_name, form):     form.vars.field1 =
table_name # I know useless, it just

because the callback function must take a single parameter, the form.
You can do instead

def my_form_processing(form):
    form.vars.field1 = form.table.name # I know useless, it just

Hope this helps.

Richard Vézina

unread,
Dec 23, 2011, 4:30:21 PM12/23/11
to web...@googlegroups.com
Thanks for explanation...

But I am unable to do what you explain because I try to make a kind of virtual table to form submission... So I need the "virtual table name" I pass by request.args(0) to access some dict that I build to make my virtual table to work...

May I request "request.args(0)" form "my_form_processing" if the only thing "my_form_processing" get is the form?

It works once, but I think it not working every time. I mean I get my own "my_form_processing" function to works with request.args(0), but it not seems to work anymore after restart web2py (local dev with rocket built-in server).

Thanks

Richard

Anthony

unread,
Dec 23, 2011, 6:28:18 PM12/23/11
to web...@googlegroups.com
If my_form_processing is defined in your controller or a model, it should have access to the request object.

overstandnigeria

unread,
Jul 2, 2014, 5:05:40 AM7/2/14
to web...@googlegroups.com
Thanks Massimo , i have used this code and its works fine ,

chaitanya

unread,
May 28, 2015, 1:16:53 AM5/28/15
to web...@googlegroups.com
Hi Iam very new to web2py.
According to my understanding, if the c value is greater than zero the c value has to be displayed.
I used the same code as yours but I did not see the c value printed.
Can you please clarify if Iam understanding it wrong.

黄祥

unread,
May 28, 2015, 4:48:47 AM5/28/15
to web...@googlegroups.com
not sure if not viewing your code but i guess, you didn't explicit print the variable c on the views.

best regards,
stifan

Anthony

unread,
May 28, 2015, 9:44:22 AM5/28/15
to web...@googlegroups.com, chaitanya....@gmail.com
No, in this case, the "c" field is neither readable nor writable, so it won't appear in forms (including readonly forms) or the grid unless explicitly added. The code in question calculates and inserts a value for "c" based on the values for "a" and "b", but this is not displayed anywhere.

Anthony
Reply all
Reply to author
Forward
0 new messages