diff --git a/htdocs/core/actions_addupdatedelete.inc.php b/htdocs/core/actions_addupdatedelete.inc.php index 6c03839a996..d2d22d74e3c 100644 --- a/htdocs/core/actions_addupdatedelete.inc.php +++ b/htdocs/core/actions_addupdatedelete.inc.php @@ -107,7 +107,7 @@ if ($action == 'add' && !empty($permissiontoadd)) { } // Validation of fields values - if ($conf->global->MAIN_FEATURE_LEVEL >= 2 || !empty($conf->global->MAIN_USE_COMMON_VALIDATION)) { + if ($conf->global->MAIN_FEATURE_LEVEL >= 2 || !empty($conf->global->MAIN_ACTIVATE_VALIDATION_RESULT)) { if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) { if (!$object->validateField($object->fields, $key, $value)) { $error++; @@ -213,6 +213,15 @@ if ($action == 'update' && !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_ACTIVATE_VALIDATION_RESULT)) { + 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 39eeae90fc2..cfba42e0adf 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -6381,9 +6381,10 @@ abstract class CommonObject * @param string|int $morecss Value for css to define style/length of field. May also be a numeric. * @param int $nonewbutton Force to not show the new button on field that are links to object * @param bool $displayValidationResult Display validation results messages for fields, set it to true after form sended to display user mistakes + * @param string $mode 1=Used for search filters * @return string */ - public function showInputField($val, $key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = 0, $nonewbutton = 0, $displayValidationResult = false) + public function showInputField($val, $key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = 0, $nonewbutton = 0, $mode = 0) { global $conf, $langs, $form; @@ -6400,17 +6401,14 @@ abstract class CommonObject if ($conf->global->MAIN_FEATURES_LEVEL < 2 && empty($conf->global->MAIN_ACTIVATE_VALIDATION_RESULT)) { $displayValidationResult = false; } // Validation tests and output - $fieldValidation = 0; // 0 not tested, -1 error, 1 success - $fieldValidationErrorMesg = ''; + $fieldValidationErrorMsg = ''; $validationClass = ''; - if ($displayValidationResult) { - if (!$this->validateField($val, $key, $value)) { + if($mode == 0){ + $fieldValidationErrorMsg = $this->getFieldError($key); + if (!empty($fieldValidationErrorMsg)) { $validationClass = ' --error'; // the -- is use as class state in css : .--error can't be be defined alone it must be define with another class like .my-class.--error or input.--error - $fieldValidation = -1; - $fieldValidationErrorMesg = $this->error; } else { $validationClass = ' --success'; // the -- is use as class state in css : .--success can't be be defined alone it must be define with another class like .my-class.--success or input.--success - $fieldValidation = 1; } } @@ -6506,6 +6504,11 @@ abstract class CommonObject } } + // Add validation state class + if (!empty($validationClass)) { + $morecss.= ' '.$validationClass; + } + if (in_array($type, array('date'))) { $tmp = explode(',', $size); $newsize = $tmp[0]; @@ -6971,6 +6974,12 @@ abstract class CommonObject if ($type == 'date') $out.=' (YYYY-MM-DD)'; elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)'; */ + + // Display error message for field + if ($mode == 0 && !empty($fieldValidationErrorMsg) && function_exists('getFieldErrorIcon')) { + $out .= ' '.getFieldErrorIcon($fieldValidationErrorMsg); + } + return $out; } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 61a3869fe1a..5dd500a26a0 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -9753,6 +9753,23 @@ function dolGetButtonTitleSeparator($moreClass = "") return ''; } +/** + * get field error icon + * + * @param string $fieldValidationErrorMsg + */ +function getFieldErrorIcon($fieldValidationErrorMsg) +{ + $out = ''; + if (!empty($fieldValidationErrorMesg)) { + $out.= ''; // role alert is used for accessibility + $out.= ''; // For accessibility icon is separated and aria-hidden + $out.= ''; + } + + return $out; +} + /** * Function dolGetButtonTitle : this kind of buttons are used in title in list *