From 578dc9adde74c913962083eb4e129ffec0ed0fc9 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 12 Jun 2021 12:58:20 +0200 Subject: [PATCH] WIP - create validation method for common object --- htdocs/core/actions_addupdatedelete.inc.php | 9 +++ htdocs/core/class/commonobject.class.php | 65 +++++++++++++++---- .../template/class/myobject.class.php | 2 +- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/htdocs/core/actions_addupdatedelete.inc.php b/htdocs/core/actions_addupdatedelete.inc.php index f871ed73de8..6c03839a996 100644 --- a/htdocs/core/actions_addupdatedelete.inc.php +++ b/htdocs/core/actions_addupdatedelete.inc.php @@ -105,6 +105,15 @@ if ($action == 'add' && !empty($permissiontoadd)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors'); } + + // Validation of fields values + if ($conf->global->MAIN_FEATURE_LEVEL >= 2 || !empty($conf->global->MAIN_USE_COMMON_VALIDATION)) { + if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) { + if (!$object->validateField($object->fields, $key, $value)) { + $error++; + } + } + } } // Fill array 'array_options' with data from add form diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index f3426608c55..39eeae90fc2 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -123,6 +123,10 @@ abstract class CommonObject */ protected $table_ref_field = ''; + /** + * @var array $validateFieldsErrors to store error results of ->validateField() + */ + public $validateFieldsErrors = array(); // Following vars are used by some objects only. We keep this property here in CommonObject to be able to provide common method using them. @@ -7305,6 +7309,40 @@ abstract class CommonObject return $out; } + /** + * clear validation message result for a field + * + * @param string $fieldKey Key of attribute to clear + */ + public function clearFieldError($fieldKey) + { + $this->error = ''; + unset($this->validateFieldsErrors[$fieldKey]); + } + + /** + * set validation error message a field + * + * @param string $fieldKey Key of attribute + */ + public function setFieldError($fieldKey, $msg = '') + { + $this->error = $this->validateFieldsErrors[$fieldKey] = $msg; + } + + /** + * get field error message + * + * @param string $fieldKey Key of attribute + */ + public function getFieldError($fieldKey) + { + if (!empty($this->validateFieldsErrors[$fieldKey])) { + return $this->validateFieldsErrors[$fieldKey]; + } + return ''; + } + /** * Return validation test result for a field * @@ -7319,8 +7357,7 @@ abstract class CommonObject if(!class_exists('Validate')){ require_once DOL_DOCUMENT_ROOT . '/core/class/validate.class.php'; } - // TODO : ask @eldy to know if need to use another error field to separate error msg - $this->error = ''; // error will be use for form error display so must be clear before + $this->clearFieldError($fieldKey); if (!isset($val[$fieldKey])) { return false; @@ -7393,7 +7430,7 @@ abstract class CommonObject // Required test and empty value if($required && !$validate->isNotEmptyString($fieldValue)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } elseif (!$required && !$validate->isNotEmptyString($fieldValue)) { @@ -7403,13 +7440,13 @@ abstract class CommonObject // MAX Size test if(!empty($maxSize) && !$validate->isMaxLength($fieldValue, $maxSize)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } // MIN Size test if(!empty($minSize) && !$validate->isMinLength($fieldValue, $minSize)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } @@ -7419,12 +7456,12 @@ abstract class CommonObject if (in_array($type, array('date', 'datetime', 'timestamp'))) { if (!$validate->isTimestamp($fieldValue)) { - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } elseif ($type == 'duration') { if(!$validate->isDuration($fieldValue)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } @@ -7432,35 +7469,35 @@ abstract class CommonObject { // is numeric if(!$validate->isDuration($fieldValue)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } elseif ($type == 'boolean') { if(!$validate->isBool($fieldValue)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } elseif ($type == 'mail') { if(!$validate->isEmail($fieldValue)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } } elseif ($type == 'url') { if(!$validate->isUrl($fieldValue)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } elseif ($type == 'phone') { if (!$validate->isPhone($fieldValue)) { - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } @@ -7484,7 +7521,7 @@ abstract class CommonObject } if(!isInDb($value_arr, $InfoFieldList[0], $selectkey)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } @@ -7495,7 +7532,7 @@ abstract class CommonObject $classname = $InfoFieldList[0]; $classpath = $InfoFieldList[1]; if(!$validate->isFetchable($fieldValue, $classname, $classpath)){ - $this->error = $validate->error; + $this->setFieldError($fieldKey, $validate->error); return false; } else { return true; } } diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index e69c5bc333d..be8736f9d49 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -91,7 +91,7 @@ class MyObject extends CommonObject * 'arrayofkeyval' to set a list of values if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar' * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1. * 'comment' is not used. You can store here any text of your choice. It is not used by application. - * + * 'validate' is 1 if need to validate with $this->validateField() * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor. */