2012-06-12 57 views
2

我有一个注册表格:银条纹。 setFormAction导致错误

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); 
..... 

这到底是怎么回事?

感谢

+0

只是澄清:你有将上面的表单和表单处理程序添加到Page类中,因为您希望在每个页面上都有它,对吗?但是,为什么你要重定向处理程序,反正你应该在每一页上都有这个功能? – xeraa

回答

1

出现这种错误,因为silverstripe如何处理形式

silverstripe也从来没有重定向到形式的行动,它总是重定向到形式,它的自我,所以如果你的表格是

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

然后银条纹会一直重定向回到这个函数,所以如果你在mysite.com/foobar/表单会去mysite.com/foobar/SignupForm 然后,它会调用 - > SignupAction(),所以SignupAction从来不是我网址。

这是我会怎么做:

<?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/