[+FEATURE] (ViewHelpers): Adjust Fluid to new Property Mapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / ErrorsViewHelper.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * Error messages view helper, which is deprecated in Extbase 1.4.0, with the old property mapper.
25 *
26 * = Examples =
27 *
28 * <code title="Output error messages as a list">
29 * <ul class="errors">
30 * <f:form.errors>
31 * <li>{error.code}: {error.message}</li>
32 * </f:form.errors>
33 * </ul>
34 * </code>
35 * <output>
36 * <ul>
37 * <li>1234567890: Validation errors for argument "newBlog"</li>
38 * </ul>
39 * </output>
40 *
41 * <code title="Output error messages for a single property">
42 * <f:form.errors for="someProperty">
43 * <div class="error">
44 * <strong>{error.propertyName}</strong>: <f:for each="{error.errors}" as="errorDetail">{errorDetail.message}</f:for>
45 * </div>
46 * </f:form.errors>
47 * </code>
48 * <output>
49 * <div class="error>
50 * <strong>someProperty:</strong> errorMessage1 errorMessage2
51 * </div>
52 * </output>
53 *
54 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
55 * @api
56 * @deprecated since Extbase 1.4.0, will be removed with Extbase 1.6.0.
57 */
58 class Tx_Fluid_ViewHelpers_Form_ErrorsViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
59
60 /**
61 * Iterates through selected errors of the request.
62 *
63 * @param string $for 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.
64 * @param string $as The name of the variable to store the current error
65 * @return string Rendered string
66 * @author Christopher Hlubek <hlubek@networkteam.com>
67 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
68 * @api
69 */
70 public function render($for = '', $as = 'error') {
71 $errors = $this->controllerContext->getRequest()->getErrors();
72 if ($for !== '') {
73 $propertyPath = explode('.', $for);
74 foreach ($propertyPath as $currentPropertyName) {
75 $errors = $this->getErrorsForProperty($currentPropertyName, $errors);
76 }
77 }
78 $output = '';
79 foreach ($errors as $errorKey => $error) {
80 $this->templateVariableContainer->add($as, $error);
81 $output .= $this->renderChildren();
82 $this->templateVariableContainer->remove($as);
83 }
84 return $output;
85 }
86
87 /**
88 * Find errors for a specific property in the given errors array
89 *
90 * @param string $propertyName The property name to look up
91 * @param array $errors An array of Tx_Fluid_Error_Error objects
92 * @return array An array of errors for $propertyName
93 * @author Christopher Hlubek <hlubek@networkteam.com>
94 */
95 protected function getErrorsForProperty($propertyName, $errors) {
96 foreach ($errors as $error) {
97 if ($error instanceof Tx_Extbase_Validation_PropertyError) {
98 if ($error->getPropertyName() === $propertyName) {
99 return $error->getErrors();
100 }
101 }
102 }
103 return array();
104 }
105 }
106
107 ?>