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.

Form Questions

Form Actions - Form posting to itself?


Reply

3 Posts   1520 Views

Avatar
dane.lowe

8 September 2009 at 7:21pm Community Member, 3 Posts

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

8 September 2009 at 9:46pm Community Member, 20 Posts

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

8 September 2009 at 10:05pm (Last edited: 8 September 2009 10:09pm), Community Member, 3 Posts

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.