Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Cancel button


Go to End


6 Posts   2962 Views

Avatar
jennymgray

Community Member, 9 Posts

17 November 2011 at 10:16pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

17 November 2011 at 11:28pm

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

Avatar
jennymgray

Community Member, 9 Posts

18 November 2011 at 12:02am

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.

Avatar
sheadawson

Community Member, 49 Posts

3 April 2012 at 5:39pm

Did you manage to find a solution for this?

Thanks :)

Avatar
jennymgray

Community Member, 9 Posts

3 April 2012 at 7:58pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

5 April 2012 at 10:58pm

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.