[BUGFIX] Remove dependency to iconv lib in form
[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 */
105 public function __construct($arguments) {
106 $this->localCobj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
107 $this->injectRequestHandler(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request'));
108 $this->localizationHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Localization');
109 $this->setFieldName($arguments['element']);
110 $this->setMessage($arguments['message.'], $arguments['message']);
111 $this->setShowMessage($arguments['showMessage']);
112 $this->setError($arguments['error.'], $arguments['error']);
113 }
114
115 /**
116 * Set the fieldName
117 *
118 * @param string $fieldName The field name
119 * @return object The rule object
120 */
121 public function setFieldName($fieldName) {
122 $this->fieldName = (string) $fieldName;
123 return $this;
124 }
125
126 /**
127 * Returns the field name
128 *
129 * @return string The field name
130 */
131 public function getFieldName() {
132 return $this->fieldName;
133 }
134
135 /**
136 * Returns the message
137 *
138 * @return array Typoscript for cObj
139 */
140 public function getMessage() {
141 return $this->message;
142 }
143
144 /**
145 * Returns the error
146 *
147 * @return array Typoscript for cObj
148 */
149 public function getError() {
150 return $this->error;
151 }
152
153 /**
154 * Set the message, like 'required' for the validation rule
155 * and substitutes markers for values, like %maximum
156 *
157 * The output will be a Typoscript array to use as cObj
158 * If no parameter is given, it will take the default locallang label
159 * If only first parameter, then it's supposed to be a TEXT cObj
160 * When both are filled, it's supposed to be a cObj made by the administrator
161 * In the last case, no markers will be substituted
162 *
163 * @param mixed $message Message as string or TS
164 * @param string $type Name of the cObj
165 * @return void
166 */
167 public function setMessage($message = '', $type = 'TEXT') {
168 if (empty($message)) {
169 if (!empty($type)) {
170 $message = $type;
171 $type = 'TEXT';
172 } else {
173 $type = 'TEXT';
174 $message = $this->getLocalLanguageLabel('message');
175 }
176 $value['value'] = $this->substituteValues($message);
177 } elseif (!is_array($message)) {
178 $value['value'] = $this->substituteValues($message);
179 } else {
180 $value = $message;
181 }
182 $this->message['cObj'] = (string) $type;
183 $this->message['cObj.'] = $value;
184 }
185
186 /**
187 * Set the error, like 'This field is required' for the validation rule
188 *
189 * The output will be a Typoscript array to use as cObj
190 * If no parameter is given, it will take the default locallang label
191 * If only first parameter, then it's supposed to be a TEXT cObj
192 * When both are filled, it's supposed to be a cObj made by the administrator
193 * In the last case, no markers will be substituted
194 *
195 * @param mixed $error Message as string or TS
196 * @param string $type Name of the cObj
197 * @return void
198 */
199 public function setError($error = '', $type = 'TEXT') {
200 if (empty($error)) {
201 if (!empty($type)) {
202 $error = $type;
203 $type = 'TEXT';
204 } else {
205 $type = 'TEXT';
206 $error = $this->getLocalLanguageLabel('error');
207 }
208 $value['value'] = $this->substituteValues($error);
209 } elseif (!is_array($error)) {
210 $value['value'] = $this->substituteValues($error);
211 } else {
212 $value = $error;
213 }
214 $this->error['cObj'] = (string) $type;
215 $this->error['cObj.'] = $value;
216 }
217
218 /**
219 * Set if message needs to be displayed
220 *
221 * @param boolean $show TRUE is display
222 * @return object The rule object
223 */
224 public function setShowMessage($show) {
225 if ($show === NULL) {
226 $this->showMessage = TRUE;
227 } else {
228 $this->showMessage = (bool) $show;
229 }
230 return $this;
231 }
232
233 /**
234 * Returns TRUE when message needs to be displayed
235 *
236 * @return boolean
237 */
238 public function messageMustBeDisplayed() {
239 return $this->showMessage;
240 }
241
242 /**
243 * Substitute makers in the message text
244 * In some cases this method will be override by rule class
245 *
246 * @param string $message Message text with markers
247 * @return string Message text with substituted markers
248 */
249 protected function substituteValues($message) {
250 return $message;
251 }
252
253 /**
254 * Get the local language label(s) for the message
255 * In some cases this method will be override by rule class
256 *
257 * @param string $type The type
258 * @return string The local language message label
259 */
260 protected function getLocalLanguageLabel($type) {
261 $label = strtolower(get_class($this)) . '.' . $type;
262 $message = $this->localizationHandler->getLocalLanguageLabel($label);
263 return $message;
264 }
265
266 }
267
268
269 ?>