Jump to:

1970 Posts in 1272 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Cancel button

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 866 Views
  • jennymgray
    Avatar
    Community Member
    7 Posts

    Cancel button Link to this post

    Hi,

    I'd like to put a cancel button on a form rather than rely on people using the back button. I've added a FormAction and coded the function so it just does redirectBack().

    My problem is that my form's validation is kicking in before the cancel function code is fired. Because the form has required fields, the validation fails, and instead of returning where they came from, the form is displayed again with errors highlighted.

    I've checked the contents of $data in my validator class, but it doesn't have anything which indicates the button pressed, so I can't return without validating.

    How can I add my cancel button? Is there some other FormAction that I've overlooked, or some other way of not running the validation?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Cancel button Link to this post

    Instead of the plain FormAction class you'll want to add an action of class ResetFormAction for a reset button.

  • jennymgray
    Avatar
    Community Member
    7 Posts

    Re: Cancel button Link to this post

    Thank you, but that's not quite what I'm after. The ResetFormAction creates an <input type = "reset" which resets the form field contents to their original values.

    I'd like my cancel button to redirect users away from the form page, but with FormAction the validation keeps getting in the way of any redirect code I write.

  • SheaDawson
    Avatar
    Community Member
    45 Posts

    Re: Cancel button Link to this post

    Did you manage to find a solution for this?

    Thanks

  • jennymgray
    Avatar
    Community Member
    7 Posts

    Re: Cancel button Link to this post

    No, I didn't. I couldn't get round the validation problem so I gave up.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Cancel button Link to this post

    The key is not to make it a button. Make a literal field that contains a plain link.

    Heres an example CancelFormAction class I knocked up - https://gist.github.com/2309951.

    866 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.