Php 银条。SETFORMATION导致错误
我有一份报名表:Php 银条。SETFORMATION导致错误,php,silverstripe,Php,Silverstripe,我有一份报名表: function SignupForm() { $fields = new FieldSet( new TextField("FirstName", "First name"), new TextField("Surname"), new EmailField("Email", "Email address") ); $submitAction = new FieldSet(
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之前,当我提交表单时(从任何地方)出现以下错误
这是怎么回事
谢谢发生此错误是因为silverstripe处理表单的方式 silverstripe从不重定向到表单操作,它总是重定向到表单本身,因此如果您的表单
function SignupForm() {
...
return new Form(...);
}
然后silverstripe将始终重定向回该函数,因此如果您在mysite.com/foobar/上,表单将转到mysite.com/foobar/SignupForm
然后,它将调用->SignupAction(),因此SignupAction永远不会出现在url中
我会这样做:
<?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',
));
}
}
仅作澄清:您已经将上述表单和表单处理程序添加到Page类中,因为您希望在每个页面上都使用它,对吗?但是,为什么要重定向处理程序,您应该在每个页面上都有可用的处理程序?
<?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',
));
}
}