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