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