form.accepts does not react if ajax used

15 views
Skip to first unread message

matkolbe

unread,
Jun 21, 2009, 9:07:46 AM6/21/09
to web2py Web Framework
Hello there,

I have a problem, validating the form does not react.

The code in the controller inputform is defined global):

inputform=FORM(TABLE(TR(B(T('Date:')),INPUT
(_type="text",_name="new_date",_class="field-width-80 field-padding-
right-10",_id="new_datepicker_id",requires=[IS_NOT_EMPTY(),IS_DATE
(format=T('%Y/%m/%d'))]),_class="tr-height-30"),
TR(B(T('Amount:')),INPUT
(_type="text",_name="new_amount",_id="new_amount_id",requires=
[IS_NOT_EMPTY(), IS_FLOAT_IN_RANGE(0,100000)],_class="field-
width-150"),_class="tr-height-30"),
TR(B(T('Description:')),INPUT
(_type="text",_name="new_description",_id="new_description_id",requires=IS_NOT_EMPTY
(), _class="field-width-150"),_class="tr-height-30"),
TR(T('Comment:'),INPUT(_type="text",_name="new-
comment",_id="new_comment_id",_class="commentfield field-width-150")),
TR("",INPUT(_type="submit",_value=T("Add new
entry")),_class="sendingbutton tr-
height-50")),_formname="form_newflexiblecost",_id="form-
newflexiblecost_id")

and the ajax function:
def insert_flexiblecost():
savesuccess="-1"
if inputform.accepts
(request.vars,session,formname='form_newflexiblecost'):
from applications.haushaltsbuch.modules.custom_timefunctions
import convert_to_timestamp
current_date=convert_to_timestamp
(inputform.vars.new_date_id.year,inputform.vars.new_date_id.month,inputform.vars.new_date_id.day,
0,0,0)
inserted_id=db.flexible_cost.insert
(book_id=session.bookid,description=inputform.vars.new_description_id,amount=inputform.vars.new_amount_id,
\

comment=inputform.vars.new_comment_id,in_date=current_date,timetable_id=session.monthdateid)
if inserted_id>0:
savesuccess="1"
else:
savesuccess="0"
elif inputform.errors:
savesuccess="0"
return savesuccess

I always get -1 returned. It seems like the accepts() is not
registering the form submit. I exactly realized like in the ebook
mentioned...

Has anyone a similar problem?

grettings,
mathew

mdipierro

unread,
Jun 21, 2009, 12:18:33 PM6/21/09
to web2py Web Framework
I checked your form with this:

def insert_flexiblecost():
if inputform.accepts
(request.vars,session,formname='form_newflexiblecost'):
print 'done'
return dict(form=inputform)

and your form works great. he problem is somewhere else.

In particular I do not see in your code the place where the form is
returned and inserted in the view. Something is wrong with the
workflow.

Massimo

matkolbe

unread,
Jun 21, 2009, 1:00:32 PM6/21/09
to web2py Web Framework
Hi Massimo ,

first of all, I hated python, cause I was trying to totally reprogram
a webapp. Then a friedn of mine, Peter (writting an article bout
web2py), I think you also know him :o), told me about your framework -
and now I love python :o)

REALLY GREAT/BRILLIANT WORK!!!

Now to the problem:
The form is global, my controller lokked like this:
controller.py:
inputform=Form(-code above-)

def index():
bla,bla
return dict(inputform=inputform)

def insert_flexiblecost():
code above

The ajax request has all params and enters the function, but as told,
the successvalue is always -1

I also tried to define the inputform inside both, the index() and the
insert_felx...() but it won't work either...

If you have no idea I'll check the code again, but I debugged already
with firefox firebug, every thing seems to be ok, no tickets created,
no invalid response...

grettings from augsburg,
matthew

mdipierro

unread,
Jun 21, 2009, 1:42:58 PM6/21/09
to web2py Web Framework
Thanks for your comments.

You can fix your problem by replacing:

if inputform.accepts
(request.vars,session,formname='form_newflexiblecost'):

with

if inputform.accepts(request.vars,formname=None):

This is because web2py does some "magic" to prevent double form
submission and this is achieved by adding some hidden fileds in the
form. Since you validate the form using an ajax request you are
bypassing all of this so you want a plain form submissions without
hidden fields.

Massimo

matkolbe

unread,
Jun 21, 2009, 4:43:43 PM6/21/09
to web2py Web Framework
Hi Massimo,

I'm that unbelievable stupied!!! It absolutely clearly described in
your ebook... I've always skipped it, don't know why... ;o)

Greetings and thanks a lot!!!

matthew

mdipierro

unread,
Jun 21, 2009, 5:32:59 PM6/21/09
to web2py Web Framework
Actually thought it was a though one to debug. I am sure it may have
helped other users as well.

Massimo

matkolbe

unread,
Jun 22, 2009, 3:46:44 AM6/22/09
to web2py Web Framework
So here is a short form of the solution for all the others :o)

You have to define a form the normal way, as a SQLForm or a normal
FORM...

If you want the form to be submitted via an ajax request you just use
the ajax function...

For validation the form in the function the ajax is calling you have
to redefine(normal way, take a look at the ebook...)

But that's the trick, if you used a customizied FORM and not a SQLFORM
you have to set the formname empty in the accepts part!!!
(formname=NONE or don't define it...)

Tht's the way it is working and will be validated, but you have to
display errors by your own, or you use the response.flash function.
Another way I used is, tho validate if the validation fails, the form
is submitted without ajax, so the base function displaying the errors
next to the field as normal.
I wrote an ajax_true_false_funcall
(Addres,SucessFunction,ErrorFunction) and one of those both function
is executed, if ajax return =1(Successfunction) or the errorfunction
if return =0...

If any needs this ajax_true_false_funcall, just send a mail...

By the way, integrating the jquery UI pack is also no problem,
including internationalization of the datepicker... :o)
Help or questions --> just write :o)

Greeting,
matthew
Reply all
Reply to author
Forward
0 new messages