Silverstripe 自定义前端表单未呈现
Silverstripe custom front end form is not rendering
我正在学习 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
,因此您应该调用它 - 这可能是问题所在...
我正在学习 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
,因此您应该调用它 - 这可能是问题所在...