我有一些自定义路由:
---
Name: mysiteroutes
---
Director:
rules:
'signup//$Action/$Data/$Form': 'SignupController'
---
Name: modelascontrollerroutes
After: '#rootroutes'
---
Director:
rules:
'': 'HomePage_Controller'
'$URLSegment/$Action/$ID': 'BaseController'
注册控制器:
class SignupController extends Page_Controller {
private static $allowed_actions = array(
'submit'
);
public function index(SS_HTTPRequest $request) {
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
EmailField::create('Email', 'Email')
),
FieldList::create(
FormAction::create('submit', 'Submit')->setAttribute('class', 'btn btn-success')
),
RequiredFields::create('Email')
);
return $this->customise(array('Form'=>$form))->renderWith(array("Signup", "Page"));
}
public function submit($data, $form = null) {
$form->addErrorMessage("Test", "Test error message",'bad');
return $this->redirectBack();
}
}
显示表单但不显示任何错误消息。当我去提交它时,它会转到(当然)SignupController / submit,返回404.我已经setFormAction(Controller::join_links(BASE_URL, "signup", 'submit'))
添加了$form
,然后数据通过了$form
是null,我无法更新它。我可能会使它成为一个实例变量但是我可以使用正确的SS路由来解决这个问题。我应该更新我的路由以获取$form
还是其他错误?
答案 0 :(得分:2)
路由中不需要$Data
和$Form
参数,这些参数将在POST数据中。
其次,您不需要在submit
中使用allowed_actions
方法,因为它不会被路由触发。
要解决这个问题你应该; 添加功能链接
public function link($action = null)
{
return $this->join_links('signup', $action);
}
因此,您将被重定向到signup/...
然后将索引函数更改为此;
public function index() {
$form = Form::create(
$this,
'', //so it will redirect you to 'signup', instead of 'signup/index'; I think it's prettier :)
FieldList::create(
EmailField::create('Email', 'Email')
),
FieldList::create(
FormAction::create('submit', 'Submit')
->setAttribute('class', 'btn btn-success')
),
RequiredFields::create('Email')
);
if($this->request->isPost())
return $form; //return the form when it gets posted
return $this->customise(array('Form'=>$form))->renderWith(array("Signup", "Page"));
}