Adding cancel button - form submission

1,968 views
Skip to first unread message

Mathias

unread,
Jul 17, 2012, 8:13:03 AM7/17/12
to web...@googlegroups.com
Hello,

I have added a CANCEL button to my SQLFORM.

Form submission prevents the correct working of it.

I have tried to avoid form submission with this code :


def new_guest_contact():
   form = SQLFORM.factory(...)   
   
   form[0][-1][1].append(TAG.BUTTON('Cancel',_onclick='ajax("%s",[],":eval");return false' %URL(r=request,f='new_contact')))
   
   if form.process().accepted:
        ...
        redirect(URL(r=request,f='new_contact_part')) 
   elif form.errors:
       response.flash = 'form has errors'
   else:
       response.flash = 'Create new guest'
  
   return dict(form=form)  


If I click the cancel button now, nothing happens anymore.


Any help is welcome.

Thanks


Mathias

Richard Vézina

unread,
Jul 17, 2012, 9:26:26 AM7/17/12
to web...@googlegroups.com
Maybe this could be enough??

{{=A(T('cancel'),_href="javascript:void(history.go(-1))")}}

It will bring you back to the precedent page, in my use case, I direct the user on a crud.read page where the user can edit the record (crud.update). Then if don't want make change anymore there is a "cancel" hyperlink that bring back the user on the crud.read page.

Richard



Mathias

--
 
 
 

Mathias Van Daele

unread,
Jul 17, 2012, 9:46:51 AM7/17/12
to web...@googlegroups.com
Thanks for your solution Richard !

I am still looking for a solution with a button component...

Mathias

2012/7/17 Richard Vézina <ml.richa...@gmail.com>:
> --
>
>
>

Mathias Van Daele

unread,
Jul 17, 2012, 11:00:59 AM7/17/12
to web...@googlegroups.com
I found a good solution for me :

form[0][-1][1].append(TAG.INPUT(_value='Cancel',_type="button",_onclick="window.location='%s';"%URL(r=request,f='new_contact')))

Mathias


2012/7/17 Mathias Van Daele <mathias...@gmail.com>:

Massimo Di Pierro

unread,
Jul 17, 2012, 1:43:08 PM7/17/12
to web...@googlegroups.com
In trunk:

form.add_button("Cancel",URL(r=request,f='new_contact'))

Massimo Di Pierro

unread,
Sep 13, 2012, 5:49:01 PM9/13/12
to web...@googlegroups.com
+1

On Thursday, 13 September 2012 12:17:17 UTC-5, Henrik Holm wrote:
Has this "add_button" been added to one of the recent releases?  I'm running 1.99.7 and I get 

  <type 'exceptions.AttributeError'> 'SQLFORM' object has no attribute 'add_button'

Maybe it's time to upgrade :-)

Thanks,
Henrik

David Sorrentino

unread,
Oct 1, 2012, 2:36:02 PM10/1/12
to web...@googlegroups.com
Hi everybody,

Sorry for re-opening this thread, but I'm trying without success to add a cancel button to a CRUD form.

Here my controller:
def insertnews():
    form
= crud.create(db.news)
    form.add_button("cancel",URL(r=request,f='index'))
   
return dict(form=form)

Here my view:
{{=form.custom.begin}}
Body:
<div>{{=form.custom.widget.body}}</div>
{{=form.custom.submit}}
{{=form.custom.cancel}}
{{=form.custom.end}}

Where am I wrong?

Cheers,
David

Massimo Di Pierro

unread,
Oct 1, 2012, 4:47:11 PM10/1/12
to web...@googlegroups.com
When you add_button('cancel',....) you do not get a form.custom.cancel. Sorry.

David Sorrentino

unread,
Oct 1, 2012, 5:25:53 PM10/1/12
to web...@googlegroups.com
No problem! :)

So, is there any method to customize the elements of a form (fields and buttons) without using a custom form?
Because I managed to customize it with form.custom... but then I can't add a cancel button.

Thanks a lot! :)
David


--
 
 
 

Carlos Hanson

unread,
Jun 13, 2013, 5:48:02 PM6/13/13
to web...@googlegroups.com, sorren...@gmail.com

On Monday, October 1, 2012 2:25:58 PM UTC-7, David Sorrentino wrote:
No problem! :)

So, is there any method to customize the elements of a form (fields and buttons) without using a custom form?
Because I managed to customize it with form.custom... but then I can't add a cancel button.

Thanks a lot! :)
David
 

I use form.custom as well. Based on this thread, I included the following in the add() function of my controller:

cancel_button = INPUT(_type='button', _value='Cancel', _onclick='window.location=\'%s\';;return false' % URL('index'))

It has the same resulting code as form.add_button('Cancel', URL('index')).

I changed the URL() function to URL('view', args=[id]) for my edit() function. Then I just add {{=cancel_button}} next to {{=form.custom.submit}}.

Carlos Hanson
Reply all
Reply to author
Forward
0 new messages