Fraser
Fraser

Reputation: 14246

Silverstripe. setFormAction causes error

I have a signup form:

function SignupForm() { 

      $fields = new FieldSet( 
         new TextField("FirstName", "First name"), 
         new TextField("Surname"), 
         new EmailField("Email", "Email address") 
      );    
   $submitAction = new FieldSet(new FormAction("SignupAction", "Sign up")); 
   $required = new RequiredFields("Email"); 

      $SignupForm = new Form($this, "SignupForm", $fields, $submitAction, $required);



      return $SignupForm; 
   }

   function SignupAction($data, $form) {

      $member = new Member(); 
      $form->saveInto($member);

      $member->write(); 

      if($group = DataObject::get_one('Group', "ID = $this->defaultGroupID")){ 
         $member->Groups()->add($group); 
         Director::redirect('thanks-for-registering/'); 
      }else{ 
         Director::redirect('registration-failed/'); 
      }

   }

Which runs fine from the homepage, however it appears on every page and sub page on the site so I need to set the form action.

I have tried adding this:

$SignupForm->setFormAction(Director::baseURL().'home/SignupAction');

before return $SignupForm and I get the following error when I submit the form (from anywhere)

Missing argument 2 for Page_Controller::SignupAction()

function SignupAction($data, $form) { 
68 
69        
70       $member = new Member(); 
71       $form->saveInto($member); 
.....

What is going on here?

Thanks

Upvotes: 2

Views: 932

Answers (1)

Zauberfisch
Zauberfisch

Reputation: 4015

this error occurs because of how silverstripe handles forms

silverstripe does never redirect to the form action, it always redirects to the form it self, so if your form is

function SignupForm() {
   ...
   return new Form(...);
}

then silverstripe will always redirect back to this function, so if you are on mysite.com/foobar/ the form will go to mysite.com/foobar/SignupForm And then, it will call ->SignupAction(), so SignupAction is never i the url.

This is how I would do it:

<?php

class Page extends SiteTree {
}

class Page_Controller extends ContentController {
    public static $allowed_actions = array(
        'SignupForm',
        'registeringThanks',
        'registrationFailed',
    );
    public function SignupForm() {
        $fields = new FieldSet(
            new TextField("FirstName", "First name"),
            new TextField("Surname"),
            new EmailField("Email", "Email address")
        );
        $actions = new FieldSet(
            new FormAction("SignupAction", "Sign up")
        );
        $validator = new RequiredFields("Email");
        // use __FUNCTION__ here so we don't have to type SignupForm again
        return new Form($this, __FUNCTION__, $fields, $actions, $validator);
    }
    public function SignupAction($data, Form $form, SS_HTTPRequest $request) {
        $member = new Member();
        $form->saveInto($member);
        $member->write();
        $home = SiteTree::get_by_link(null);
        // instead of using $group = DataObject::get_one('Group', "ID = {$home->defaultGroupID}");
        // we can just use $group = $home->defaultGroup(); if defaultGroup is a has_one relation
        if ($home && $home->defaultGroup()) {
            $member->Groups()->add($home->defaultGroup());
            $this->redirect('registeringThanks/');
        } else {
            $this->redirect('registrationFailed/');
        }
    }
    public function registeringThanks(SS_HTTPRequest $request) {
        // display the same page again, but overwrite the Title and Content
        return $this->customise(array(
            'Title' => 'Thank you!',
            'Content' => 'All sorts of spam mails are on there way to you',
        ));
    }
    public function registrationFailed(SS_HTTPRequest $request) {
        // display the same page again, but overwrite the Title and Content
        return $this->customise(array(
            'Title' => 'Great, ... you broke it!',
            'Content' => 'Sorry, we can\'t send you any spam because something went wrong',
        ));
    }
}

NOTE: I have not tested this code, I've just written this on top of my head, you might find spelling mistakes or other minor mistakes in it) (in this case you don't need to redirect to home, the group stuff, the thankyou page and all work on every page this way)

if you have any further questions, feel free to ask here or on http://irc.silverstripe.org/

Upvotes: 1

Related Questions