4e857ae2ff105074afe5c8f80c97e6dcde6ed18e
[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
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 */
57 class Tx_Fluid_ViewHelpers_Form_ErrorsViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
58
59 /**
60 * Iterates through selected errors of the request.
61 *
62 * @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.
63 * @param string $as The name of the variable to store the current error
64 * @return string Rendered string
65 * @author Christopher Hlubek <hlubek@networkteam.com>
66 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
67 * @api
68 */
69 public function render($for = '', $as = 'error') {
70 $errors = $this->controllerContext->getRequest()->getErrors();
71 if ($for !== '') {
72 $propertyPath = explode('.', $for);
73 foreach ($propertyPath as $currentPropertyName) {
74 $errors = $this->getErrorsForProperty($currentPropertyName, $errors);
75 }
76 }
77 $output = '';
78 foreach ($errors as $errorKey => $error) {
79 $this->templateVariableContainer->add($as, $error);
80 $output .= $this->renderChildren();
81 $this->templateVariableContainer->remove($as);
82 }
83 return $output;
84 }
85
86 /**
87 * Find errors for a specific property in the given errors array
88 *
89 * @param string $propertyName The property name to look up
90 * @param array $errors An array of Tx_Fluid_Error_Error objects
91 * @return array An array of errors for $propertyName
92 * @author Christopher Hlubek <hlubek@networkteam.com>
93 */
94 protected function getErrorsForProperty($propertyName, $errors) {
95 foreach ($errors as $error) {
96 if ($error instanceof Tx_Extbase_Validation_PropertyError) {
97 if ($error->getPropertyName() === $propertyName) {
98 return $error->getErrors();
99 }
100 }
101 }
102 return array();
103 }
104 }
105
106 ?>