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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

Form Actions - Form posting to itself?

Go to End

3 Posts   1842 Views


Community Member, 3 Posts

8 September 2009 at 7:21pm


I've just chosen to learn SilverStripe by building a quick client site using it. It's been surprisingly painless to learn, except for one thing which I can't work out.

How does setting a Form's Action work? Does it require prototype.js since the action is assigned to the button rather than the form?

I built the below (to subscribe people to a mailchimp database after first creating a unique 'key' for them) by following the wiki and then following a forum post about replacing prototype with jquery.

function MailchimpSubscribeForm() {
	// Create fields
	$fields = new FieldSet(
		new TextField('Name'),
		new EmailField('Email')
	// Create actions
	$actions = new FieldSet(
		new FormAction('doMailchimpSubscribeForm', 'Subscribe')		
	$validator = new RequiredFields('Name', 'Email');
	$form = new Form($this, 'MailchimpSubscribeForm', $fields, $actions, $validator);

      // set the custom script for this form 
            $(document).ready(function() { 
	return $form->forTemplate(); 
	function doMailchimpSubscribeForm($data, $form) {
		//create key
		//attempt to subscribe to list
		//return response

When the form is submitted, it goes to /home/MailchimpSubscribeForm and the form is displayed again. The code in doMailchimpSubscribeForm() is never run.

What am I missing here?


Community Member, 20 Posts

8 September 2009 at 9:46pm

Never used $form->forTemplate() myself but I notice that you are returning that instead of the form.

Also, found this on the Form page of the docs for SS:

"First of all, you need to create your form on it’s own class, that way you can define a custom template using a forTemplate() method on your Form class. "


Community Member, 3 Posts

8 September 2009 at 10:05pm

Edited: 08/09/2009 10:09pm


The reason I had used forTemplate() was to get it to render as HTML when pulled in through the content editor. I put forTemplate in the wrong place though.

In the controller I had:

function Content() {
	return str_replace('$MailchimpSubscribeForm', $this->MailChimpSubscribeForm(), $this->Content);

I have now changed this to:

function Content() {
	return str_replace('$MailchimpSubscribeForm', $this->MailChimpSubscribeForm()->forTemplate(), $this->Content);

and removed ->forTemplate from the MailchimpSubscribeForm function:

return $form;

Now all works as expected.

Of course it now makes sense as the form object returned from this function is passed through to the doMailchimpSubscribeForm action. In my original code it would have been passing an HTML string instead of the form object.