[TASK] Merge submodule version into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Utility / ValidatorUtility.php
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Patrick Broens (patrick@patrickbroens.nl)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Static methods for validation
29 *
30 * @author Patrick Broens <patrick@patrickbroens.nl>
31 */
32 class ValidatorUtility implements \TYPO3\CMS\Core\SingletonInterface {
33
34 /**
35 * Validation objects to use
36 *
37 * @var array
38 */
39 protected $rules = array();
40
41 /**
42 * Messages for all form fields
43 *
44 * @var array
45 */
46 protected $messages = array();
47
48 /**
49 * Errors for all form fields
50 *
51 * @var unknown_type
52 */
53 protected $errors = array();
54
55 /**
56 * Constructor
57 */
58 public function __construct() {}
59
60 /**
61 * Returns the current prefix of the form
62 * The RequestHandler is asked
63 *
64 * @return String
65 */
66 protected function getPrefix() {
67 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request')->getPrefix();
68 }
69
70 /**
71 * Create a rule object according to class
72 * and sent some arguments
73 *
74 * @param string $class Name of the validation rule
75 * @param array $arguments Configuration of the rule
76 * @return \TYPO3\CMS\Form\Validation\AbstractValidator The rule object
77 */
78 public function createRule($class, $arguments = array()) {
79 $class = strtolower((string) $class);
80 $className = 'TYPO3\\CMS\\Form\\Validation\\' . ucfirst($class) . 'Validator';
81 $rule = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className, $arguments);
82 return $rule;
83 }
84
85 /**
86 * Add a rule to the rules array
87 * The rule needs to be completely configured before adding it to the array
88 *
89 * @param object $rule Rule object
90 * @param string $fieldName Field name the rule belongs to
91 * @param boolean $breakOnError Break the rule chain when TRUE
92 * @return \TYPO3\CMS\Form\Utility\ValidatorUtility
93 */
94 public function addRule($rule, $fieldName, $breakOnError = FALSE) {
95 $prefix = $this->getPrefix();
96 $this->rules[$prefix][] = array(
97 'instance' => (object) $rule,
98 'fieldName' => (string) $fieldName,
99 'breakOnError' => (bool) $breakOnError
100 );
101 if ($rule->messageMustBeDisplayed()) {
102 if (!isset($this->messages[$prefix][$fieldName])) {
103 $this->messages[$prefix][$fieldName] = array();
104 }
105 end($this->rules[$prefix]);
106 $key = key($this->rules[$prefix]);
107 $message = $rule->getMessage();
108 $this->messages[$prefix][$fieldName][$key][$key + 1] = $message['cObj'];
109 $this->messages[$prefix][$fieldName][$key][($key + 1) . '.'] = $message['cObj.'];
110 }
111 return $this;
112 }
113
114 /**
115 * Returns TRUE when each rule in the chain returns valid
116 * When a rule has breakOnError set and the rule does not validate,
117 * the check for the remaining rules will stop and method returns FALSE
118 *
119 * @return boolean True if all rules validate
120 */
121 public function isValid() {
122 $prefix = $this->getPrefix();
123 $this->errors[$prefix] = array();
124 $result = TRUE;
125 foreach ($this->rules[$prefix] as $key => $element) {
126 $rule = $element['instance'];
127 $fieldName = $element['fieldName'];
128 if ($rule->isValid()) {
129 continue;
130 }
131 $result = FALSE;
132 if (!isset($this->errors[$prefix][$fieldName])) {
133 $this->errors[$prefix][$fieldName] = array();
134 }
135 $error = $rule->getError();
136 $this->errors[$prefix][$fieldName][$key][$key + 1] = $error['cObj'];
137 $this->errors[$prefix][$fieldName][$key][($key + 1) . '.'] = $error['cObj.'];
138 if ($element['breakOnError']) {
139 break;
140 }
141 }
142 return $result;
143 }
144
145 /**
146 * Returns all messages from all rules
147 *
148 * @return array
149 */
150 public function getMessages() {
151 return $this->messages[$this->getPrefix()];
152 }
153
154 /**
155 * Returns messages for a single form object
156 *
157 * @param string $name Name of the form object
158 * @return array
159 */
160 public function getMessagesByName($name) {
161 return $this->messages[$this->getPrefix()][$name];
162 }
163
164 /**
165 * Returns TRUE when a form object has a message
166 *
167 * @param string $name Name of the form object
168 * @return boolean
169 */
170 public function hasMessage($name) {
171 if (isset($this->messages[$this->getPrefix()][$name])) {
172 return TRUE;
173 }
174 return FALSE;
175 }
176
177 /**
178 * Returns all error messages from all rules
179 *
180 * @return array
181 */
182 public function getErrors() {
183 return $this->errors[$this->getPrefix()];
184 }
185
186 /**
187 * Returns error messages for a single form object
188 *
189 * @param string $name Name of the form object
190 * @return array
191 */
192 public function getErrorsByName($name) {
193 return $this->errors[$this->getPrefix()][$name];
194 }
195
196 /**
197 * Returns TRUE when a form object has an error
198 *
199 * @param string $name Name of the form object
200 * @return boolean
201 */
202 public function hasErrors($name) {
203 if (isset($this->errors[$this->getPrefix()][$name])) {
204 return TRUE;
205 }
206 return FALSE;
207 }
208
209 }
210
211 ?>