dolibarr 23.0.3
commonfield.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2025 Open-Dsi <support@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php';
25require_once DOL_DOCUMENT_ROOT . '/core/class/validate.class.php';
26
27
31abstract class CommonField
32{
36 public $db;
37
41 public $error = '';
42
46 public $errors = array();
47
51 public $type;
52
56 public $label;
57
61 public static $form;
62
66 public static $validator;
67
71 public $emptyValues = array('', 0, array());
72
73
79 public function __construct($db)
80 {
81 global $form, $langs;
82
83 $this->db = $db;
84 $this->error = '';
85 $this->errors = array();
86
87 // Type and label
88 $this->type = strtolower(substr(get_class($this), 0, -5));
89 $this->label = 'FieldLabel' . ucfirst($this->type);
90
91 if (!isset(self::$form)) {
92 if (!is_object($form)) {
93 $form = new Form($this->db);
94 }
95 self::setForm($form);
96 }
97
98 if (!isset(self::$validator)) {
99 // Use Validate class to allow external Modules to use data validation part instead of concentrate all test here (factoring) or just for reuse
100 $validator = new Validate($this->db, $langs);
101 self::setValidator($validator);
102 }
103 }
104
111 public static function setForm(&$form)
112 {
113 self::$form = &$form;
114 }
115
122 public static function setValidator(&$validator)
123 {
124 self::$validator = &$validator;
125 }
126
132 public function clearErrors()
133 {
134 $this->error = '';
135 $this->errors = array();
136 }
137
144 public function errorsToString($separator = ', ')
145 {
146 return $this->error . (is_array($this->errors) ? (!empty($this->error) ? $separator : '') . implode($separator, $this->errors) : '');
147 }
148
157 public function isEmptyValue($fieldInfos, $value, $emptyValues = null)
158 {
159 if (!isset($value)) {
160 return true;
161 }
162
163 if (!is_array($emptyValues)) {
164 $emptyValues = !empty($fieldInfos->emptyValues) && is_array($fieldInfos->emptyValues) ? $fieldInfos->emptyValues : $this->emptyValues;
165 }
166
167 foreach ($emptyValues as $val) {
168 if ($val === $value) {
169 return true;
170 }
171 }
172
173 return false;
174 }
175
188 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
189 {
190 return '';
191 }
192
205 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
206 {
207 return '';
208 }
209
222 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
223 {
224 return '';
225 }
226
236 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = 'minwidth400')
237 {
238 if (empty($moreCss)) {
239 if (!empty($fieldInfos->css)) {
240 $moreCss = $fieldInfos->css;
241 } elseif (!empty($defaultCss)) {
242 $moreCss = $defaultCss;
243 }
244 }
245 $moreCss = trim((string) $moreCss);
246
247 return empty($moreCss) ? '' : ' ' . $moreCss;
248 }
249
259 public function verifyFieldValue($fieldInfos, $key, $value)
260 {
261 global $langs;
262
263 $required = $fieldInfos->required;
264 $minLength = $fieldInfos->minLength ?? 0;
265 $maxLength = $fieldInfos->maxLength ?? 0;
266 //$emptyValues = !empty($fieldInfos->emptyValues) ? $fieldInfos->emptyValues : $this->emptyValues;
267
268 // Clear error
269 self::$validator->error = '';
270
271 // Todo move this in validate class ?
272 // Required test and empty value
273 if ($this->isEmptyValue($fieldInfos, $value)) {
274 if ($required) {
275 self::$validator->error = $langs->trans('RequireANotEmptyValue');
276 return false;
277 } else {
278 // if no value sent and the field is not mandatory, no need to perform tests
279 return true;
280 }
281 }
282
283 // MIN Size test
284 if (!empty($minLength) && is_string($value) && !self::$validator->isMinLength($value, $minLength)) {
285 return false;
286 }
287
288 // MAX Size test
289 if (!empty($maxLength) && is_string($value) && !self::$validator->isMaxLength($value, $maxLength)) {
290 return false;
291 }
292
293 // Todo move this in validate class ?
294 // MIN Value test
295 if (isset($fieldInfos->minValue) && is_numeric($value) && ((double) $value) < $fieldInfos->minValue) {
296 self::$validator->error = $langs->trans('RequireMinValue', $fieldInfos->minValue);
297 return false;
298 }
299
300 // MAX Value test
301 if (isset($fieldInfos->maxValue) && is_numeric($value) && ((double) $value) > $fieldInfos->maxValue) {
302 self::$validator->error = $langs->trans('RequireMaxValue', $fieldInfos->maxValue);
303 return false;
304 }
305
306 return true;
307 }
308
319 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
320 {
321 $htmlName = $keyPrefix . $key . $keySuffix;
322
323 return $this->verifyFieldValue($fieldInfos, $key, GETPOST($htmlName, 'restricthtml'));
324 }
325
337 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
338 {
339 return $defaultValue;
340 }
341
353 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
354 {
355 return $defaultValue;
356 }
357
367 public function sqlFilterSearchField($fieldInfos, $key, $value)
368 {
369 return '';
370 }
371}
Class to common field.
getInputCss($fieldInfos, $moreCss='', $defaultCss='minwidth400')
Get input CSS.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
errorsToString($separator=', ')
Method to output saved errors.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
clearErrors()
clear errors
static setForm(&$form)
Set form used for print the field.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
static setValidator(&$validator)
Set validator used for check the field value.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
__construct($db)
Constructor.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to manage generation of HTML components Only common components must be here.
Class toolbox to validate values.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:125