1cd4e61f6eb3f9a0e89654d40a6878f06ff7ed3b
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / AbstractValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008 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 * Abstract for validation
28 *
29 * @author Patrick Broens <patrick@patrickbroens.nl>
30 * @package TYPO3
31 * @subpackage form
32 */
33 abstract class AbstractValidator implements \TYPO3\CMS\Form\Validation\ValidatorInterface {
34
35 /**
36 * The name of the field for the rule
37 *
38 * @var string
39 */
40 protected $fieldName;
41
42 /**
43 * Message of the rule as cObj
44 * This variable will contain two keys:
45 * $message['cObj] = TEXT
46 * $message['cObj.] = array('value', 'This is the message')
47 *
48 * @var array
49 */
50 protected $message = array();
51
52 /**
53 * Possible error message after validation as cObj
54 * This variable will contain two keys:
55 * $message['cObj] = TEXT
56 * $message['cObj.] = array('value', 'This is the error')
57 *
58 * @var array
59 */
60 protected $error = array();
61
62 /**
63 * Display the message like mandatory
64 *
65 * @var boolean
66 */
67 protected $showMessage = TRUE;
68
69 /**
70 * Localization handler object
71 *
72 * @var \TYPO3\CMS\Form\Localization
73 */
74 protected $localizationHandler;
75
76 /**
77 * Request handler object
78 *
79 * @var \TYPO3\CMS\Form\Request
80 */
81 protected $requestHandler;
82
83 /**
84 * The content object
85 *
86 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
87 */
88 protected $localCobj;
89
90 /**
91 * Inject the Request Handler
92 *
93 * @param \TYPO3\CMS\Form\Request $requestHandler
94 * @return void
95 */
96 public function injectRequestHandler(\TYPO3\CMS\Form\Request $requestHandler) {
97 $this->requestHandler = $requestHandler;
98 }
99
100 /**
101 * Constructor
102 *
103 * @param array $arguments Typoscript configuration for the validation rule
104 * @return void
105 */
106 public function __construct($arguments) {
107 $this->localCobj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
108 $this->injectRequestHandler(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request'));
109 $this->localizationHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Localization');
110 $this->setFieldName($arguments['element']);
111 $this->setMessage($arguments['message.'], $arguments['message']);
112 $this->setShowMessage($arguments['showMessage']);
113 $this->setError($arguments['error.'], $arguments['error']);
114 }
115
116 /**
117 * Set the fieldName
118 *
119 * @param string $fieldName The field name
120 * @return object The rule object
121 */
122 public function setFieldName($fieldName) {
123 $this->fieldName = (string) $fieldName;
124 return $this;
125 }
126
127 /**
128 * Returns the field name
129 *
130 * @return string The field name
131 */
132 public function getFieldName() {
133 return $this->fieldName;
134 }
135
136 /**
137 * Returns the message
138 *
139 * @return array Typoscript for cObj
140 */
141 public function getMessage() {
142 return $this->message;
143 }
144
145 /**
146 * Returns the error
147 *
148 * @return array Typoscript for cObj
149 */
150 public function getError() {
151 return $this->error;
152 }
153
154 /**
155 * Set the message, like 'required' for the validation rule
156 * and substitutes markers for values, like %maximum
157 *
158 * The output will be a Typoscript array to use as cObj
159 * If no parameter is given, it will take the default locallang label
160 * If only first parameter, then it's supposed to be a TEXT cObj
161 * When both are filled, it's supposed to be a cObj made by the administrator
162 * In the last case, no markers will be substituted
163 *
164 * @param mixed $message Message as string or TS
165 * @param string $type Name of the cObj
166 * @return void
167 */
168 public function setMessage($message = '', $type = 'TEXT') {
169 if (empty($message)) {
170 if (!empty($type)) {
171 $message = $type;
172 $type = 'TEXT';
173 } else {
174 $type = 'TEXT';
175 $message = $this->getLocalLanguageLabel('message');
176 }
177 $value['value'] = $this->substituteValues($message);
178 } elseif (!is_array($message)) {
179 $value['value'] = $this->substituteValues($message);
180 } else {
181 $value = $message;
182 }
183 $this->message['cObj'] = (string) $type;
184 $this->message['cObj.'] = $value;
185 }
186
187 /**
188 * Set the error, like 'This field is required' for the validation rule
189 *
190 * The output will be a Typoscript array to use as cObj
191 * If no parameter is given, it will take the default locallang label
192 * If only first parameter, then it's supposed to be a TEXT cObj
193 * When both are filled, it's supposed to be a cObj made by the administrator
194 * In the last case, no markers will be substituted
195 *
196 * @param mixed $error Message as string or TS
197 * @param string $type Name of the cObj
198 * @return void
199 */
200 public function setError($error = '', $type = 'TEXT') {
201 if (empty($error)) {
202 if (!empty($type)) {
203 $error = $type;
204 $type = 'TEXT';
205 } else {
206 $type = 'TEXT';
207 $error = $this->getLocalLanguageLabel('error');
208 }
209 $value['value'] = $this->substituteValues($error);
210 } elseif (!is_array($error)) {
211 $value['value'] = $this->substituteValues($error);
212 } else {
213 $value = $error;
214 }
215 $this->error['cObj'] = (string) $type;
216 $this->error['cObj.'] = $value;
217 }
218
219 /**
220 * Set if message needs to be displayed
221 *
222 * @param boolean $show TRUE is display
223 * @return object The rule object
224 */
225 public function setShowMessage($show) {
226 if ($show === NULL) {
227 $this->showMessage = TRUE;
228 } else {
229 $this->showMessage = (bool) $show;
230 }
231 return $this;
232 }
233
234 /**
235 * Returns TRUE when message needs to be displayed
236 *
237 * @return boolean
238 */
239 public function messageMustBeDisplayed() {
240 return $this->showMessage;
241 }
242
243 /**
244 * Substitute makers in the message text
245 * In some cases this method will be override by rule class
246 *
247 * @param string $message Message text with markers
248 * @return string Message text with substituted markers
249 */
250 protected function substituteValues($message) {
251 return $message;
252 }
253
254 /**
255 * Get the local language label(s) for the message
256 * In some cases this method will be override by rule class
257 *
258 * @param string $type The type
259 * @return string The local language message label
260 */
261 protected function getLocalLanguageLabel($type) {
262 $label = strtolower(get_class($this)) . '.' . $type;
263 $message = $this->localizationHandler->getLocalLanguageLabel($label);
264 return $message;
265 }
266
267 }
268
269
270 ?>