Custom form with jquery acts weird

117 views
Skip to first unread message

Jordan Ladora

unread,
Sep 17, 2013, 3:15:01 PM9/17/13
to web...@googlegroups.com
Hi,

I have the same problem as

https://groups.google.com/forum/?fromgroups#!searchin/web2py/custom$20form$20jquery/web2py/7Meea7Ul0o8/4ZdrD9hP5MEJ
https://groups.google.com/forum/?fromgroups#!searchin/web2py/custom$20form$20jquery/web2py/E9RyEqh01RQ/6duW4RmALvcJ

From the first thread/link above, "...if I use a regular form, {{=form}}, then validation works but not if I use the custom form. Also form.process() works, so the problem seems to be related to custom forms.."

Those threads seemed to end without finding a solution. I think the author of those threads didn't realize that the form is actually being validated, but it doesn't seem like it b/c the error messages do not show with the custom form.

In my hands, if I use a custom form, it validates OK but form errors do not display and, quite often, any jquery code in the view misbehaves (which is the real problem). This behavior turns on and off depending ONLY on whether I have a custom form. I used {{=custom.form.begin}} and {{=custom.form.end}} and {{=form.custom.widget.xyz}} and {{=form.custom.label.xyz}} in the view. If I replace it with {{=form}} then everything works perfectly.

In the controller, I used

if form.accepts(request, session, onvalidation=abc):
  ...


Thanks for any help. I love the custom forms and it really stinks not being able to use them with jQuery!!

-jl

ps - I previously posted with more details about this problem, before I realized the problem seems related more generally to javascript/jquery + custom forms -

https://groups.google.com/forum/?fromgroups=#!topic/web2py/rPHKPqqHbsg
https://groups.google.com/forum/?fromgroups=#!topic/web2py/0LpfMJPwSsI


Jordan Ladora

unread,
Sep 20, 2013, 5:43:46 PM9/20/13
to
Yes, I will pack a minimal application & post it here over the weekend - thanks for the help!!!

Marin Pranjić

unread,
Sep 20, 2013, 5:39:54 PM9/20/13
to web2py-users
Can you pack a minimal application that reproduces this?


On Fri, Sep 20, 2013 at 11:18 PM, Jordan Ladora <vicepres...@gmail.com> wrote:
Ahhhhhhh f*ck it, then...




On Tuesday, September 17, 2013 12:15:01 PM UTC-7, Jordan Ladora wrote:

--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jordan Ladora

unread,
Sep 26, 2013, 3:44:49 PM9/26/13
to web...@googlegroups.com
Hello again,

I've attached an app with the problem I have.. if you click on the button and go to 'debug_js' view, you'll see that hitting the 'cancel' button there causes a POST request, form reload, and validation. This is par for the problems I've been having with custom forms.. if you move that button outside the custom form, everything behaves normally..

Thanks again for help with this!
-jl



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/rHwdHamBjHI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
web2py.app.debug_js_092513.w2p

Massimo Di Pierro

unread,
Sep 27, 2013, 5:39:16 PM9/27/13
to web...@googlegroups.com
Actually the problem you have is different. The links you refer to both had the same problem and it was addressed by Anthony in his post. You cannot do:

form=crud.create(db.comments)
if form.process().accepted

because crud.create already calls process() internally, you it was called twice.

You problem is different. You have a button inside a form and if you click it, it will submit the form. Unless you tell it not to:

<button onclick="event.preventDefault();window.location='{{=URL('index')}}'">Cancel</button>

Jordan Ladora

unread,
Sep 27, 2013, 9:00:47 PM9/27/13
to web...@googlegroups.com
Hi Massimo,

Thanks for your help. I think I'm crystal clear on your explanation regarding the button, but I'm still confused on the other issue that Anthony answered previously, despite his and your posts..

In my controller I used:

  form = SQLFORM.factory(*fields)

  if form.accepts(request, session):


Could you please be a little more specific? What should I use here instead of 'form.accepts' ? I think this is causing me trouble elsewhere in my application, too, and it would be really helpful if I knew a little bit more..

Thanks again,
-J




On Tuesday, September 17, 2013 12:15:01 PM UTC-7, Jordan Ladora wrote:

Anthony

unread,
Sep 27, 2013, 10:36:04 PM9/27/13
to web...@googlegroups.com
If you follow Massimo's suggestion:

<button onclick="event.preventDefault();window.location='{{=URL('index')}}'">Cancel</button>

everything seems to work fine. What problem do you have after making that change?

Anthony

Jordan Ladora

unread,
Sep 28, 2013, 12:51:10 PM9/28/13
to web...@googlegroups.com
Hi Anthony,

Thanks for your note. Like I said, I understand the helpful comments on fixing the <button onclick...> issue.

However, what is unclear to me still is Massimo's (and yours here - https://groups.google.com/forum/?fromgroups#!searchin/web2py/custom$20form$20jquery/web2py/7Meea7Ul0o8/4ZdrD9hP5MEJ ) comments on form processing.. Massimo pointed out that, a la Jonas' post, that you cannot do -

form=crud.create(db.comments)
if form.process().accepted

...yet I have:


  form = SQLFORM.factory(*fields)
  if form.accepts(request, session):

...and that also seems to be wrong (no doubt, since process() is a shortcut for accepts(), they are doing the same thing). BUT I am not sure how to correct it - you both said that process is getting called twice. OK, if I shouldn't use 'if form.accepts(request, session): ' then what should I use to process (and call validation on) the form once the user clicks 'Submit' ?

Thanks again,
-jl

Anthony

unread,
Sep 28, 2013, 1:10:24 PM9/28/13
to

form=crud.create(db.comments)
if form.process().accepted

Right, you cannot do that. 

 
  form = SQLFORM.factory(*fields)
  if form.accepts(request, session):

The above is perfectly fine (though form.process().accepted is now preferred to form.accepts(request, session)).

My question was exactly what problem are you having with the above? The app you attached uses the above, and it works fine.

Anthony

Jordan Ladora

unread,
Sep 28, 2013, 5:49:28 PM9/28/13
to web...@googlegroups.com
Thanks for that - I'd assumed the button in the form was related, but let me try and isolate the other problem and make another app that reproduces it.




On Saturday, September 28, 2013 10:09:58 AM UTC-7, Anthony wrote:

form=crud.create(db.comments)
if form.process().accepted

Right, you cannot do that. 

 
  form = SQLFORM.factory(*fields)
  if form.accepts(request, session):

Reply all
Reply to author
Forward
0 new messages