WIP - create validation method for common object

This commit is contained in:
ATM john 2021-06-12 14:16:40 +02:00
parent 578dc9adde
commit 43ecba2e63
3 changed files with 44 additions and 9 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -9753,6 +9753,23 @@ function dolGetButtonTitleSeparator($moreClass = "")
return '<span class="button-title-separator '.$moreClass.'" ></span>';
}
/**
* get field error icon
*
* @param string $fieldValidationErrorMsg
*/
function getFieldErrorIcon($fieldValidationErrorMsg)
{
$out = '';
if (!empty($fieldValidationErrorMesg)) {
$out.= '<span class="field-error-icon fa fa-exclamation-circle classfortooltip" title="'.dol_escape_htmltag($fieldValidationErrorMsg, 1).'" role="alert" >'; // role alert is used for accessibility
$out.= '<span class="fa fa-exclamation-circle" aria-hidden="true" ></span>'; // For accessibility icon is separated and aria-hidden
$out.= '</span>';
}
return $out;
}
/**
* Function dolGetButtonTitle : this kind of buttons are used in title in list
*