我创建ContactPage.php
类型,并使用自定义模板ContactForm.php
创建表单类ContactForm.ss
。提交表格后,没有任何反应。我的错误在哪里?
ContactForm.php:
class ContactForm extends Form {
public function __construct($controller, $name, $arguments = array()) {
$fields = new FieldList();
$fields->push(TextField::create('From', 'Email'));
$fields->push(TextField::create('Subject', 'Tema'));
$fields->push(TextField::create('Message', 'Message'));
$actions = new FieldList(FormAction::create('doSendMessage')->setTitle('Kontakt'));
$validator = new RequiredFields('From', 'Subject', 'Message');
parent::__construct($controller, $name, $fields, $actions, $validator);
}
public function doSendMessage($data, Form $form) {
// Test
Controller::curr()->redirect('http://www.google.com');
}
public function forTemplate() {
return $this->renderWith([
$this->class,
'Form'
]);
}
}
ContactPage.php:
class ContactPage extends Page {
}
class ContactPage_Controller extends Page_Controller {
private static $allowed_actions = [
'MyContactForm'
];
public function init() {
parent::init(); // TODO: Change the autogenerated stub
}
public function MyContactForm() {
return new ContactForm($this, 'MyContactForm');
}
}
ContactForm.ss:
<form $FormAttributes>
<label for="{$FormName}_Email">Email adresa</label>
$Fields.dataFieldByName(From)
<label for="{$FormName}_Email">Tema</label>
$Fields.dataFieldByName(Subject)
<label for="{$FormName}_Email">Poruka</label>
$Fields.dataFieldByName(Message)
$Fields.dataFieldByName(SecurityID)
<% if $Actions %>
<% loop $Actions %>
$Field
<% end_loop %>
<% end_if %>
</form>
答案 0 :(得分:0)
我使用此代码安装了SilverStripe 3.5版本,它对我有用。当您提交时,它将重定向到Google,提示doSendMessage
函数正在触发。
您缺少ContactPage
的版式。在template / Layout目录中,您需要创建一个名为ContactPage.ss
使用类似的内容:
<% include SideBar %>
<div class="content-container unit size3of4 lastUnit">
<article>
<h1>$Title</h1>
<div class="content">$Content</div>
</article>
$Form
$CommentsForm
$MyContactForm
</div>
重要的是拥有$MyContactForm
令牌。