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

Form Actions - Form posting to itself?


Go to End


3 Posts   2354 Views

Avatar
dane.lowe

Community Member, 3 Posts

8 September 2009 at 7:21pm

Hi,

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);
	$form->loadDataFrom($this->failover); 

      // set the custom script for this form 
      Requirements::customScript(' 
            $(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?

Avatar
zalzadore

Community Member, 20 Posts

8 September 2009 at 9:46pm

Dane,
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. "

Avatar
dane.lowe

Community Member, 3 Posts

8 September 2009 at 10:05pm

Edited: 08/09/2009 10:09pm

Thanks!

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.