TYPO3 API  SVNRelease
ErrorsViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * Error messages view helper
00025  *
00026  * = Examples =
00027  *
00028  * <code title="Output error messages as a list">
00029  * <ul class="errors">
00030  *   <f:form.errors>
00031  *     <li>{error.code}: {error.message}</li>
00032  *   </f:form.errors>
00033  * </ul>
00034  * </code>
00035  * <output>
00036  * <ul>
00037  *   <li>1234567890: Validation errors for argument "newBlog"</li>
00038  * </ul>
00039  * </output>
00040  *
00041  * <code title="Output error messages for a single property">
00042  * <f:form.errors for="someProperty">
00043  *   <div class="error">
00044  *     <strong>{error.propertyName}</strong>: <f:for each="{error.errors}" as="errorDetail">{errorDetail.message}</f:for>
00045  *   </div>
00046  * </f:form.errors>
00047  * </code>
00048  * <output>
00049  * <div class="error>
00050  *   <strong>someProperty:</strong> errorMessage1 errorMessage2
00051  * </div>
00052  * </output>
00053  *
00054  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00055  * @api
00056  */
00057 class Tx_Fluid_ViewHelpers_Form_ErrorsViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00058 
00059     /**
00060      * Iterates through selected errors of the request.
00061      *
00062      * @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.
00063      * @param string $as The name of the variable to store the current error
00064      * @return string Rendered string
00065      * @author Christopher Hlubek <hlubek@networkteam.com>
00066      * @author Sebastian Kurfürst <sebastian@typo3.org>
00067      * @api
00068      */
00069     public function render($for = '', $as = 'error') {
00070         $errors = $this->controllerContext->getRequest()->getErrors();
00071         if ($for !== '') {
00072             $propertyPath = explode('.', $for);
00073             foreach ($propertyPath as $currentPropertyName) {
00074                 $errors = $this->getErrorsForProperty($currentPropertyName, $errors);
00075             }
00076         }
00077         $output = '';
00078         foreach ($errors as $errorKey => $error) {
00079             $this->templateVariableContainer->add($as, $error);
00080             $output .= $this->renderChildren();
00081             $this->templateVariableContainer->remove($as);
00082         }
00083         return $output;
00084     }
00085 
00086     /**
00087      * Find errors for a specific property in the given errors array
00088      *
00089      * @param string $propertyName The property name to look up
00090      * @param array $errors An array of Tx_Fluid_Error_Error objects
00091      * @return array An array of errors for $propertyName
00092      * @author Christopher Hlubek <hlubek@networkteam.com>
00093      */
00094     protected function getErrorsForProperty($propertyName, $errors) {
00095         foreach ($errors as $error) {
00096             if ($error instanceof Tx_Extbase_Validation_PropertyError) {
00097                 if ($error->getPropertyName() === $propertyName) {
00098                     return $error->getErrors();
00099                 }
00100             }
00101         }
00102         return array();
00103     }
00104 }
00105 
00106 ?>