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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Form Confirmation


Go to End


4 Posts   590 Views

Avatar
Bagzli

Community Member, 71 Posts

15 March 2015 at 11:13am

Edited: 15/03/2015 11:19am

Is there a way to have a pop up with a yes/no comes when a player clicks submit on a form? If user clicks yes then it executes the form post, if user clicks no then it cancels it, how would I achieve this with silverstripe forms?

Or even better, is there a way I can tell it to execute a custom java script function prior to submitting the form? Then if result returns true submit, else do nothing.

Avatar
JonoM

Community Member, 130 Posts

17 March 2015 at 8:08am

Hi Bagzli,

If you are talking about doing this on the front-end of a website, you can include any javascript you want wherever your form is rendered by using the Requirements class. http://doc.silverstripe.org/en/developer_guides/templates/requirements The fact that the form is generated by SilverStripe doesn't matter, you can use the same kind of javascript you would for any HTML form.

If you're talking about doing it in the CMS you can include extra javascript the same way - add a javascript Requirements call in your getCMSFields() function then follow this guide to see how to use jQuery Entwine to bind your validation behaviour to your form: http://doc.silverstripe.org/en/developer_guides/customising_the_admin_interface/javascript_development

Avatar
Bagzli

Community Member, 71 Posts

17 March 2015 at 1:57pm

In order to do that, I need to have something like this

<form onsubmit="return confirm('Do you really want to submit the form?');">

the problem is that the form tag is auto generated, so how do I inject my javascript to tell it to call a function prior to submitting?

Avatar
JonoM

Community Member, 130 Posts

17 March 2015 at 2:17pm

Rather than inlining your javascript on the form, consider attaching the behaviour by targeting the element you want in separate script. This is easy to do with jQuery or you can do it with native javascript doing something like:

var form = document.getElementById('MyForm'); // ID of the form you want to target - SilverStripe will automatically generate an ID on the form
form.onsubmit = function() {
    //Do stuff here
};

Note that you would have to fire this script after the form is loaded in to the DOM, either by placing it in a script tag after the form, or firing the code in a window.onload or similar.

If you want to use jQuery see http://api.jquery.com/submit/ and http://api.jquery.com/ready/