[TASK] Streamline phpdoc annotations in EXT:fluid
[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
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
20
21 /**
22 * Validation results view helper
23 *
24 * = Examples =
25 *
26 * <code title="Output error messages as a list">
27 * <f:form.validationResults>
28 * <f:if condition="{validationResults.flattenedErrors}">
29 * <ul class="errors">
30 * <f:for each="{validationResults.flattenedErrors}" as="errors" key="propertyPath">
31 * <li>{propertyPath}
32 * <ul>
33 * <f:for each="{errors}" as="error">
34 * <li>{error.code}: {error}</li>
35 * </f:for>
36 * </ul>
37 * </li>
38 * </f:for>
39 * </ul>
40 * </f:if>
41 * </f:form.validationResults>
42 * </code>
43 * <output>
44 * <ul class="errors">
45 * <li>1234567890: Validation errors for argument "newBlog"</li>
46 * </ul>
47 * </output>
48 *
49 * <code title="Output error messages for a single property">
50 * <f:form.validationResults for="someProperty">
51 * <f:if condition="{validationResults.flattenedErrors}">
52 * <ul class="errors">
53 * <f:for each="{validationResults.errors}" as="error">
54 * <li>{error.code}: {error}</li>
55 * </f:for>
56 * </ul>
57 * </f:if>
58 * </f:form.validationResults>
59 * </code>
60 * <output>
61 * <ul class="errors">
62 * <li>1234567890: Some error message</li>
63 * </ul>
64 * </output>
65 */
66 class ValidationResultsViewHelper extends 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 public function initializeArguments()
81 {
82 $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, '');
83 $this->registerArgument('as', 'string', 'The name of the variable to store the current error', false, 'validationResults');
84 }
85
86 /**
87 * @param array $arguments
88 * @param \Closure $renderChildrenClosure
89 * @param RenderingContextInterface $renderingContext
90 * @return mixed
91 */
92 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
93 {
94 $templateVariableContainer = $renderingContext->getVariableProvider();
95 $controllerContext = $renderingContext->getcontrollerContext();
96 $for = $arguments['for'];
97 $as = $arguments['as'];
98
99 $validationResults = $controllerContext->getRequest()->getOriginalRequestMappingResults();
100 if ($validationResults !== null && $for !== '') {
101 $validationResults = $validationResults->forProperty($for);
102 }
103 $templateVariableContainer->add($as, $validationResults);
104 $output = $renderChildrenClosure();
105 $templateVariableContainer->remove($as);
106 return $output;
107 }
108 }