Silverstripe自定义前端表单不呈现

问题描述 投票:1回答:1

我正在学习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>

它不是渲染表单。我该如何呈现表单?

silverstripe silverstripe-4
1个回答
1
投票

在您的ArticlePage.ss中,您正在调用$ContactForm,但在您的控制器中它被命名为CommentForm,所以你应该调用它 - 这可能是问题...

© www.soinside.com 2019 - 2024. All rights reserved.