2

我有一个注册表单:

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/'); 
      }

   }

从主页运行良好,但它出现在网站的每个页面和子页面上,因此我需要设置表单操作。

我试过添加这个:

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

在返回 $SignupForm 之前,当我(从任何地方)提交表单时出现以下错误

Missing argument 2 for Page_Controller::SignupAction()

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

这里发生了什么?

谢谢

4

1 回答 1

1

发生此错误是因为 silverstripe 处理表单的方式

silverstripe 永远不会重定向到表单操作,它总是重定向到它自己的表单,所以如果你的表单是

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

那么 silverstripe 将始终重定向回这个函数,所以如果你在 mysite.com/foobar/ 上,表单会转到 mysite.com/foobar/SignupForm 然后,它会调用 ->SignupAction(),所以 SignupAction 永远不会 i网址。

我会这样做:

<?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',
        ));
    }
}

注意:我没有测试过这段代码,我只是把它写在我的头上,你可能会发现它的拼写错误或其他小错误)(在这种情况下你不需要重定向到主页,组的东西,感谢页面,所有页面都以这种方式工作)

如果您还有任何问题,请随时在此处或在http://irc.silverstripe.org/上提问

于 2012-08-20T18:19:24.700 回答