Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Form Actions - Form posting to itself?

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

Page: 1
Go to End
Author Topic: 1454 Views
  • dane.lowe
    Avatar
    Community Member
    3 Posts

    Form Actions - Form posting to itself? Link to this post

    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?

  • zalzadore
    Avatar
    Community Member
    20 Posts

    Re: Form Actions - Form posting to itself? Link to this post

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

  • dane.lowe
    Avatar
    Community Member
    3 Posts

    Re: Form Actions - Form posting to itself? Link to this post

    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.

    1454 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.