Skip to content

Commit 928cf98

Browse files
[TASK] Update f:form.submit ViewHelper page (#117)
* [TASK] Update f:form.submit ViewHelper page Releases: main, 13.4 * Apply suggestions from code review --------- Co-authored-by: Garvin Hicking <[email protected]> (cherry picked from commit 27f3c54)
1 parent 170dcb9 commit 928cf98

File tree

6 files changed

+159
-16
lines changed

6 files changed

+159
-16
lines changed

Documentation/Global/Form/Index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ data objects.
4444
For example, user could add a comment in such a form:
4545

4646
.. literalinclude:: _codesnippets/_CommentForm.html
47-
:caption: packages/my_extension/Resources/Private/Templates/Comment/_CommentForm.html
47+
:caption: packages/my_extension/Resources/Private/Templates/Comment/CommentForm.html
4848

4949
The Extbase Controller action displaying the form then creates the Domain object
5050
and passes it to the view. In the Fluid template above we use argument

Documentation/Global/Form/Submit.rst

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,85 @@ Form.submit ViewHelper `<f:form.submit>`
99

1010
.. typo3:viewhelper:: form.submit
1111
:source: ../../Global.json
12-
:display: tags,description,gitHubLink,arguments
12+
:display: tags
13+
:noindex:
14+
15+
Creates a submit button (`<input type="submit"...>`) within a
16+
`Form ViewHelper <f:form> <https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form>`_.
17+
18+
.. note::
19+
If you you need a `<button>` with extended HTML content, use the
20+
`Form.button ViewHelper <f:form.button> <https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form-button>`_
21+
instead.
22+
23+
.. typo3:viewhelper:: form.submit
24+
:source: ../../Global.json
25+
:display: gitHubLink
26+
:noindex:
27+
28+
.. contents:: Table of contents
1329

1430
.. _typo3-fluid-form-submit-example:
1531

16-
Examples
17-
========
32+
A Fluid form with a single submit button
33+
========================================
34+
35+
You can use the `<f:form.submit value="Submit!">` button within an Extbase
36+
form to display a `<input type="submit" value="Submit!">` button.
37+
38+
When the user clicks the button, the action specified by the surrounding
39+
`<f:form> <https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form>`_
40+
is called.
41+
42+
.. tabs::
1843

19-
Defaults
20-
--------
44+
.. group-tab:: Fluid
2145

22-
::
46+
.. literalinclude:: _codesnippets/_FormSubmit.html
47+
:caption: packages/my_extension/Resources/Private/Templates/Comment/Edit.html
2348

24-
<f:form.submit value="Send Mail" />
49+
.. group-tab:: Controller
2550

26-
Output::
51+
The controller action can then look like this:
2752

28-
<input type="submit" />
53+
.. literalinclude:: _codesnippets/_SubmitController.php
54+
:caption: packages/my_extension/Classes/Controller/CommentController.php
2955

30-
Dummy content for template preview
31-
----------------------------------
56+
.. _typo3-fluid-form-submit-example-multiple:
3257

33-
::
58+
A Fluid form with multiple submit buttons
59+
=========================================
3460

35-
<f:form.submit name="mySubmit" value="Send Mail"><button>dummy button</button></f:form.submit>
61+
When you want to offer different actions, it can be helpful to use multiple
62+
submit buttons with different labels:
3663

37-
Output::
64+
.. tabs::
3865

39-
<input type="submit" name="mySubmit" value="Send Mail" />
66+
.. group-tab:: Fluid
67+
68+
.. literalinclude:: _codesnippets/_FormSubmitMultiple.html
69+
:caption: packages/my_extension/Resources/Private/Templates/Comment/Edit.html
70+
71+
.. group-tab:: Controller
72+
73+
The controller action can then look like this:
74+
75+
.. literalinclude:: _codesnippets/_SubmitMultipleController.php
76+
:caption: packages/my_extension/Classes/Controller/CommentController.php
77+
78+
.. note::
79+
All rendered buttons will be rendered as `<input type="submit"...>`.
80+
If you need a button with a different type than "submit", use the
81+
`Form.button ViewHelper <f:form.button> <https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-form-button>`_
82+
instead.
83+
84+
.. _typo3-fluid-form-submit-arguments:
85+
86+
Arguments of the form.submit ViewHelper
87+
=======================================
88+
89+
.. include:: /_Includes/_ArbitraryArguments.rst.txt
90+
91+
.. typo3:viewhelper:: form.submit
92+
:source: /Global.json
93+
:display: arguments-only
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
2+
data-namespace-typo3-fluid="true">
3+
<f:form action="submit" controller="Comment" objectName="comment" object="{comment}" method="post">
4+
<label for="tx-blogexample-content">Message:</label>
5+
<f:form.textarea property="content" id="tx-blogexample-content" rows="8" cols="46"/>
6+
<f:form.submit value="Submit"/>
7+
</f:form>
8+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
2+
data-namespace-typo3-fluid="true">
3+
<f:form action="submit" controller="MyController" objectName="comment" object="{comment}" method="post">
4+
<label for="tx-blogexample-content">Message:</label>
5+
<f:form.textarea property="content" id="tx-blogexample-content" rows="8" cols="46"/>
6+
<f:form.submit name="submitButton" value="Submit"/>
7+
<f:form.submit name="cancelButton" value="Cancel"/>
8+
<f:form.submit name="spellingButton" value="Check spelling"/>
9+
</f:form>
10+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Controller;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use T3docs\BlogExample\Domain\Model\Comment;
9+
use T3docs\BlogExample\Domain\Repository\CommentRepository;
10+
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
11+
12+
class CommentController extends ActionController
13+
{
14+
public function __construct(
15+
protected readonly CommentRepository $commentRepository,
16+
) {}
17+
18+
public function submitAction(Comment $comment): ResponseInterface
19+
{
20+
$this->commentRepository->update($comment);
21+
$this->addFlashMessage('Your comment was saved');
22+
return $this->redirect('show');
23+
}
24+
25+
// Other actions
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Controller;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use T3docs\BlogExample\Domain\Model\Comment;
9+
use T3docs\BlogExample\Domain\Repository\CommentRepository;
10+
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
11+
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
12+
13+
class CommentController extends ActionController
14+
{
15+
public function __construct(
16+
protected readonly CommentRepository $commentRepository,
17+
) {}
18+
19+
public function submitAction(
20+
Comment $comment,
21+
bool $cancelButton = false,
22+
bool $spellingButton = false,
23+
): ResponseInterface {
24+
if ($cancelButton) {
25+
$this->addFlashMessage(
26+
'Your comment was NOT saved, you pressed the cancel button',
27+
'Attention',
28+
ContextualFeedbackSeverity::WARNING,
29+
);
30+
return $this->redirect('show');
31+
}
32+
if ($spellingButton) {
33+
if (!$this->mySpellCheckService->check($comment->getMessage())) {
34+
$this->addFlashMessage('There are spelling errors. ');
35+
}
36+
return $this->redirect('edit', null, null, ['comment' => $comment]);
37+
}
38+
// Form was submitted by submit button or for example by JavaScript
39+
$this->commentRepository->update($comment);
40+
$this->addFlashMessage('Your comment was saved');
41+
return $this->redirect('show');
42+
}
43+
44+
// Other actions
45+
}

0 commit comments

Comments
 (0)