Form.hidden ViewHelper <f:form.hidden>

ViewHelper which renders an <input type="hidden" ...> tag.

Go to the source code of this ViewHelper: Form\HiddenViewHelper.php (GitHub).

Hidden field bound to a controller argument

Hidden fields can be used to pass additional arguments to a controller action.

The form includes a hidden field to pass the blog post UID:

packages/my_extension/Resources/Private/Templates/Comment/Form.html
<f:form action="save" controller="Comment" method="post">
    <f:form.hidden name="commentId" value="{comment.id}" />
    <f:form.textfield name="message" value="{comment.message}" />
    <f:form.submit value="Save" />
</f:form>
Copied!

The controller action can then look like this:

packages/my_extension/Classes/Controller/CommentController.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Controller;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class NewsletterController extends ActionController
{
    public function orderNewsletterAction(
        // If the checkbox is not checked, no argument is provided.
        // Default to false
        bool $orderNewsletter = false,
    ): ResponseInterface {
        if ($orderNewsletter) {
            // TODO: Newsletter ordering
        }
        return $this->htmlResponse();
    }
}
Copied!

Use case: Creating a comment for a specific blog post

You have:

  • A Comment domain model that relates to a BlogPost
  • A BlogPost detail page that includes a comment form
  • The property blogPost is set automatically and not selectable by the user
packages/my_extension/Resources/Private/Templates/Comment/CreateForm.html
<f:form action="create" controller="Comment" method="post" object="{comment}">
    <f:form.hidden property="blogPost" value="{blogPost.uid}" />
    <f:form.textfield property="authorName" />
    <f:form.textarea property="content" />
    <f:form.submit value="Submit Comment" />
</f:form>
Copied!

The domain model includes a relation to the BlogPost entity:

packages/my_extension/Classes/Domain/Model/Comment.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class Comment extends AbstractEntity
{
    protected string $authorName = '';
    protected string $content = '';
    protected BlogPost $blogPost;

    // Getters and setters...
}
Copied!

The controller saves the comment and redirects to the related blog post:

packages/my_extension/Classes/Controller/CommentController.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Controller;

use MyVendor\MyExtension\Domain\Model\Comment;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class CommentController extends ActionController
{
    public function createAction(Comment $comment): ResponseInterface
    {
        // $comment->getBlogPost() returns the associated BlogPost object
        $this->commentRepository->add($comment);
        $this->addFlashMessage('Your comment was submitted!');
        return $this->redirect('show', 'BlogPost', null, ['blogPost' => $comment->getBlogPost()]);
    }
}
Copied!

Arguments of the form hidden ViewHelper

additionalAttributes

additionalAttributes
Type
array
Additional tag attributes. They will be added directly to the resulting HTML tag.

aria

aria
Type
array
Additional aria-* attributes. They will each be added with a "aria-" prefix.

data

data
Type
array
Additional data-* attributes. They will each be added with a "data-" prefix.

name

name
Type
string
Name of input tag

property

property
Type
string
Name of Object Property. If used in conjunction with <f:form object="...">, the "name" property will be ignored, while "value" can be used to specify a default field value instead of the object property value.

respectSubmittedDataValue

respectSubmittedDataValue
Type
bool
Default
true
enable or disable the usage of the submitted values

value

value
Type
mixed
Value of input tag