我正在学习SilverStripe并创建自定义前端文件。但我无法在视图中呈现表单。
这是我的ArticlePageController.php
class ArticlePageController extends PageController
{
private static $allowed_actions = [
'CommentForm'
];
public function CommentForm()
{
$form = Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('Name', '')->setAttribute('placeholder', 'Name*'),
EmailField::create('Email', '')->setAttribute('placeholder', 'Email*'),
TextareaField::create('Comment', '')->setAttribute('placeholder', 'Comment*')
),
FieldList::create(
FormAction::create('handleComment', 'Post Comment')
),
RequiredFields::create('Name', 'Email', 'Comment')
);
return $form;
}
public function handleComment($data, $form)
{
$comment = ArticleComment::create();
$comment->ArticlePageID = $this->ID;
$form->saveInfo($comment);
$comment->write();
$form->sessionMessage('Thanks for your comment!', 'good');
return $this->redirectBack();
}
}
我在ArticlePage.php中添加了这个
private static $has_many = [
'Comments' => ArticleComment::class,
];
这是ArticleComment.php
class ArticleComment extends DataObject
{
private static $db = [
'Name' => 'Varchar',
'Email' => 'Varchar',
'Comment' => 'Text'
];
private static $has_one = [
'ArticlePage' => ArticlePage::class,
];
}
在ArticlePage.ss中,我尝试将表单呈现如下。
<h1>Post Your Comment</h1>
<div>
$ContactForm
</div>
它不是渲染表单。我该如何呈现表单?
在您的ArticlePage.ss
中,您正在调用$ContactForm
,但在您的控制器中它被命名为CommentForm
,所以你应该调用它 - 这可能是问题...