[TASK] Convert remaining ViewHelpers to static calling
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / ValidationResultsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
17 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
18
19 /**
20 * Validation results view helper
21 *
22 * = Examples =
23 *
24 * <code title="Output error messages as a list">
25 * <f:form.validationResults>
26 * <f:if condition="{validationResults.flattenedErrors}">
27 * <ul class="errors">
28 * <f:for each="{validationResults.flattenedErrors}" as="errors" key="propertyPath">
29 * <li>{propertyPath}
30 * <ul>
31 * <f:for each="{errors}" as="error">
32 * <li>{error.code}: {error}</li>
33 * </f:for>
34 * </ul>
35 * </li>
36 * </f:for>
37 * </ul>
38 * </f:if>
39 * </f:form.validationResults>
40 * </code>
41 * <output>
42 * <ul class="errors">
43 * <li>1234567890: Validation errors for argument "newBlog"</li>
44 * </ul>
45 * </output>
46 *
47 * <code title="Output error messages for a single property">
48 * <f:form.validationResults for="someProperty">
49 * <f:if condition="{validationResults.flattenedErrors}">
50 * <ul class="errors">
51 * <f:for each="{validationResults.errors}" as="error">
52 * <li>{error.code}: {error}</li>
53 * </f:for>
54 * </ul>
55 * </f:if>
56 * </f:form.validationResults>
57 * </code>
58 * <output>
59 * <ul class="errors">
60 * <li>1234567890: Some error message</li>
61 * </ul>
62 * </output>
63 *
64 * @api
65 */
66 class ValidationResultsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
67 {
68 use CompileWithRenderStatic;
69
70 /**
71 * As this ViewHelper renders HTML, the output must not be escaped.
72 *
73 * @var bool
74 */
75 protected $escapeOutput = false;
76
77 /**
78 * Initialize arguments
79 *
80 * @api
81 */
82 public function initializeArguments()
83 {
84 parent::initializeArguments();
85 $this->registerArgument('for', 'string', 'The name of the error name (e.g. argument name or property name). This can also be a property path (like blog.title), and will then only display the validation errors of that property.', false, '');
86 $this->registerArgument('as', 'string', 'The name of the variable to store the current error', false, 'validationResults');
87 }
88
89 /**
90 * @param array $arguments
91 * @param \Closure $renderChildrenClosure
92 * @param RenderingContextInterface $renderingContext
93 * @return mixed
94 */
95 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
96 {
97 $templateVariableContainer = $renderingContext->getVariableProvider();
98 $controllerContext = $renderingContext->getcontrollerContext();
99 $for = $arguments['for'];
100 $as = $arguments['as'];
101
102 $validationResults = $controllerContext->getRequest()->getOriginalRequestMappingResults();
103 if ($validationResults !== null && $for !== '') {
104 $validationResults = $validationResults->forProperty($for);
105 }
106 $templateVariableContainer->add($as, $validationResults);
107 $output = $renderChildrenClosure();
108 $templateVariableContainer->remove($as);
109 return $output;
110 }
111 }