Wai Yan Hein
Wai Yan Hein

Reputation: 14851

Silverstripe custom front end form is not rendering

I am learning SilverStripe and creating a custom front end file. But I cannot render the form in the view.

This is my 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();
    }
}

I added this in the ArticlePage.php

private static $has_many = [
        'Comments' => ArticleComment::class,
    ];

This is the ArticleComment.php

class ArticleComment extends DataObject
{
    private static $db = [
        'Name' => 'Varchar',
        'Email' => 'Varchar',
        'Comment' => 'Text'
    ];

    private static $has_one = [
        'ArticlePage' => ArticlePage::class,
    ];
}

In the ArticlePage.ss, I try to render the form as follow.

<h1>Post Your Comment</h1>
<div>
$ContactForm
</div>

It is not rendering the form. How can I render the form?

Upvotes: 1

Views: 119

Answers (1)

ifusion
ifusion

Reputation: 2233

In your ArticlePage.ss you are calling $ContactForm, but in your controller it is named CommentForm, so you should be calling that - that might be the issue...

Upvotes: 1

Related Questions