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).
Table of contents
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:
The controller action can then look like this:
<?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();
}
}
Tip
In a <f:
with the method
set to post
, the content of hidden fields is sent in the POST body.
This is ideal for data that should not be visible in the URL or is too
large to include in the query string.
Use case: Creating a comment for a specific blog post
You have:
- A
Comment
domain model that relates to aBlog
Post - A
Blog
detail page that includes a comment formPost - The property
blog
is set automatically and not selectable by the userPost
The domain model includes a relation to the BlogPost entity:
The controller saves the comment and redirects to the related blog post:
<?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()]);
}
}
Tip
Use a hidden field to bind a new object (like a Comment
) to its parent
(like a Blog
). This keeps the relation intact without exposing
it in the user interface.
In this example, the blog
property is set via a hidden input and
automatically mapped back to a domain object using Extbase's property
mapping.
Arguments of the form hidden ViewHelper
additionalAttributes
-
- Type
- array
Additional tag attributes. They will be added directly to the resulting HTML tag.
aria
-
- Type
- array
Additional aria-* attributes. They will each be added with a "aria-" prefix.
data
-
- Type
- array
Additional data-* attributes. They will each be added with a "data-" prefix.
name
-
- Type
- string
Name of input tag
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
-
- Type
- bool
- Default
- true
enable or disable the usage of the submitted values
value
-
- Type
- mixed
Value of input tag