Open form action in new window/tab only when validation passed

32 views
Skip to first unread message

Yan Wong

unread,
Apr 17, 2020, 4:09:58 AM4/17/20
to web2py-users
I have page containing a web2py form, and the page is meant to be viewed in an iframe. However, if the form validates, I need to escape the iframe by opening the action URL in a new tab, e.g. using target="_blank" (this is a redirect to a PayPal payments page, which is why it will not work in an iframe). However, if the form does *not* validate, I want to stay within the iframe and reload the form elements with the validation recommendations shown. I can't figure out how to do this.

It seems like I should abstract the form into a .load component, and use the web2py LOAD function to keep reloading the form into the embedded page, which presumably uses the onsubmit event to reload the component. But I can't work out how to change the form target to "_blank" *after* successful validation. Has anyone come across this problem before, and are there any solutions?

Thanks!

Yan

Yan Wong

unread,
Apr 18, 2020, 10:21:37 AM4/18/20
to web2py-users
I think I've worked out what I *want* to do, but I'm not sure if this is easy in web2py. I want to use the onsubmit handler in a form to reload the form from a .load view, but return false so that the final submit action is not called. Then, if the reloaded form validates, I can set response.js in the loaded view to actually call .submit() on the form. As I understand, this will *not* trigger the onsubmit event, but it will go to the url specified in the action. That way I can do, e.g.

<form action="some_other_url" target="_blank" onsubmit="reload_form(); return false;">
...
<input type="submit" />
</form>


and only if the form validates will clicking [submit] open up a new tab containing some_other_url. Otherwise, the form will be reloaded in-situ using AJAX. Is this a reasonable way to proceed, and is there any web2py magic that I could use to help with this? Or will I need to code by hand all the AJAX reloading in the `reload_form()` function shown above, risking duplicating the web2py LOAD() functionality?
Reply all
Reply to author
Forward
0 new messages