WIP - create validation method for common object
This commit is contained in:
parent
801f6cab2d
commit
578dc9adde
@ -105,6 +105,15 @@ if ($action == 'add' && !empty($permissiontoadd)) {
|
|||||||
$error++;
|
$error++;
|
||||||
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
|
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
|
// Fill array 'array_options' with data from add form
|
||||||
|
|||||||
@ -123,6 +123,10 @@ abstract class CommonObject
|
|||||||
*/
|
*/
|
||||||
protected $table_ref_field = '';
|
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.
|
// 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;
|
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
|
* 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'; }
|
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->clearFieldError($fieldKey);
|
||||||
$this->error = ''; // error will be use for form error display so must be clear before
|
|
||||||
|
|
||||||
if (!isset($val[$fieldKey])) {
|
if (!isset($val[$fieldKey])) {
|
||||||
return false;
|
return false;
|
||||||
@ -7393,7 +7430,7 @@ abstract class CommonObject
|
|||||||
|
|
||||||
// Required test and empty value
|
// Required test and empty value
|
||||||
if($required && !$validate->isNotEmptyString($fieldValue)){
|
if($required && !$validate->isNotEmptyString($fieldValue)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
elseif (!$required && !$validate->isNotEmptyString($fieldValue)) {
|
elseif (!$required && !$validate->isNotEmptyString($fieldValue)) {
|
||||||
@ -7403,13 +7440,13 @@ abstract class CommonObject
|
|||||||
|
|
||||||
// MAX Size test
|
// MAX Size test
|
||||||
if(!empty($maxSize) && !$validate->isMaxLength($fieldValue, $maxSize)){
|
if(!empty($maxSize) && !$validate->isMaxLength($fieldValue, $maxSize)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// MIN Size test
|
// MIN Size test
|
||||||
if(!empty($minSize) && !$validate->isMinLength($fieldValue, $minSize)){
|
if(!empty($minSize) && !$validate->isMinLength($fieldValue, $minSize)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -7419,12 +7456,12 @@ abstract class CommonObject
|
|||||||
|
|
||||||
if (in_array($type, array('date', 'datetime', 'timestamp'))) {
|
if (in_array($type, array('date', 'datetime', 'timestamp'))) {
|
||||||
if (!$validate->isTimestamp($fieldValue)) {
|
if (!$validate->isTimestamp($fieldValue)) {
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
} elseif ($type == 'duration') {
|
} elseif ($type == 'duration') {
|
||||||
if(!$validate->isDuration($fieldValue)){
|
if(!$validate->isDuration($fieldValue)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
@ -7432,35 +7469,35 @@ abstract class CommonObject
|
|||||||
{
|
{
|
||||||
// is numeric
|
// is numeric
|
||||||
if(!$validate->isDuration($fieldValue)){
|
if(!$validate->isDuration($fieldValue)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
elseif ($type == 'boolean')
|
elseif ($type == 'boolean')
|
||||||
{
|
{
|
||||||
if(!$validate->isBool($fieldValue)){
|
if(!$validate->isBool($fieldValue)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
elseif ($type == 'mail')
|
elseif ($type == 'mail')
|
||||||
{
|
{
|
||||||
if(!$validate->isEmail($fieldValue)){
|
if(!$validate->isEmail($fieldValue)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
elseif ($type == 'url')
|
elseif ($type == 'url')
|
||||||
{
|
{
|
||||||
if(!$validate->isUrl($fieldValue)){
|
if(!$validate->isUrl($fieldValue)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
elseif ($type == 'phone')
|
elseif ($type == 'phone')
|
||||||
{
|
{
|
||||||
if (!$validate->isPhone($fieldValue)) {
|
if (!$validate->isPhone($fieldValue)) {
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
@ -7484,7 +7521,7 @@ abstract class CommonObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!isInDb($value_arr, $InfoFieldList[0], $selectkey)){
|
if(!isInDb($value_arr, $InfoFieldList[0], $selectkey)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
@ -7495,7 +7532,7 @@ abstract class CommonObject
|
|||||||
$classname = $InfoFieldList[0];
|
$classname = $InfoFieldList[0];
|
||||||
$classpath = $InfoFieldList[1];
|
$classpath = $InfoFieldList[1];
|
||||||
if(!$validate->isFetchable($fieldValue, $classname, $classpath)){
|
if(!$validate->isFetchable($fieldValue, $classname, $classpath)){
|
||||||
$this->error = $validate->error;
|
$this->setFieldError($fieldKey, $validate->error);
|
||||||
return false;
|
return false;
|
||||||
} else { return true; }
|
} else { return true; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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'
|
* '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.
|
* '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.
|
* '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.
|
* Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user