many value validation improvements, use constants for validation errors

This commit is contained in:
Uwe Steinmann 2021-09-16 16:11:13 +02:00
parent cd1800c19d
commit ffe4b50c7f

View File

@ -443,6 +443,25 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
const objtype_document = '2';
const objtype_documentcontent = '3';
/*
* The validation error codes
*/
const val_error_none = 0;
const val_error_min_values = 1;
const val_error_max_values = 2;
const val_error_boolean = 8;
const val_error_int = 6;
const val_error_date = 9;
const val_error_float = 7;
const val_error_regex = 3;
const val_error_email = 5;
const val_error_url = 4;
const val_error_document = 10;
const val_error_folder = 11;
const val_error_user = 12;
const val_error_group = 13;
const val_error_valueset = 14;
/**
* Constructor
*
@ -1161,36 +1180,38 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
}
/* Turn $attrvalue into an array of values. Checks if $attrvalue starts
* with a separator char as set in the value set and uses it to explode
* with a separator char as set in the value set and use it to explode
* the $attrvalue. If the separator doesn't match or this attribute
* definition doesn't have a value set, then just create a one element
* array. if $attrvalue is empty, then create an empty array.
*/
if($this->getMultipleValues()) {
if(is_string($attrvalue)) {
if(is_string($attrvalue) && $attrvalue) {
$sep = $attrvalue[0];
$vsep = $this->getValueSetSeparator();
if($sep == $vsep)
$values = explode($attrvalue[0], substr($attrvalue, 1));
else
$values = array($attrvalue);
} elseif(is_string($attrvalue) && !$attrvalue) {
$values = array();
} else
$values = $attrvalue;
} elseif($attrvalue) {
$values = array($attrvalue);
} elseif($attrvalue !== null) {
$values = array($attrvalue);
} else {
$values = array();
}
/* Check if attribute value has at least the minimum number of values */
$this->_validation_error = 0;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_none;
if($this->getMinValues() > count($values)) {
$this->_validation_error = 1;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_min_values;
return false;
}
/* Check if attribute value has not more than maximum number of values */
if($this->getMaxValues() && $this->getMaxValues() < count($values)) {
$this->_validation_error = 2;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_max_values;
return false;
}
@ -1198,55 +1219,56 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
switch((string) $this->getType()) {
case self::type_boolean:
foreach($values as $value) {
$success &= preg_match('/^[01]$/', $value) ? true : false;
$success = $success && (preg_match('/^[01]$/', $value) ? true : false);
}
if(!$success)
$this->_validation_error = 8;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_boolean;
break;
case self::type_int:
foreach($values as $value) {
$success &= preg_match('/^[0-9]*$/', $value) ? true : false;
$success = $success && (preg_match('/^[0-9]*$/', $value) ? true : false);
}
if(!$success)
$this->_validation_error = 6;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_int;
break;
case self::type_date:
foreach($values as $value) {
$success &= preg_match('/^[12][0-9]{3}-[01][0-9]-[0-9]{2}$/', $value) ? true : false;
$d = explode('-', $value, 3);
$success = $success && (count($d) == 3)&& checkdate((int) $d[1], (int) $d[2], (int) $d[0]);
}
if(!$success)
$this->_validation_error = 9;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_date;
break;
case self::type_float:
foreach($values as $value) {
$success &= is_numeric($value);
$success = $success && is_numeric($value);
}
if(!$success)
$this->_validation_error = 7;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_float;
break;
case self::type_string:
if(trim($this->getRegex()) != '') {
foreach($values as $value) {
$success &= preg_match($this->getRegex(), $value) ? true : false;
$success = $success && (preg_match($this->getRegex(), $value) ? true : false);
}
}
if(!$success)
$this->_validation_error = 3;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_regex;
break;
case self::type_email:
foreach($values as $value) {
//$success &= filter_var($value, FILTER_VALIDATE_EMAIL) ? true : false;
$success &= preg_match('/^[a-z0-9._-]+@+[a-z0-9._-]+\.+[a-z]{2,63}$/i', $value);
$success = $success && (preg_match('/^[a-z0-9._-]+@[a-z0-9-]{2,63}(\.[a-z0-9-]{2,63})*\.[a-z]{2,63}$/i', $value) ? true : false);
}
if(!$success)
$this->_validation_error = 5;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_email;
break;
case self::type_url:
foreach($values as $value) {
$success &= preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $value);
$success = $success && (preg_match('/^http(s)?:\/\/[a-z0-9_-]+(\.[a-z0-9-]{2,63})*(:[0-9]+)?(\/.*)?$/i', $value) ? true : false);
}
if(!$success)
$this->_validation_error = 4;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_url;
break;
case self::type_document:
foreach($values as $value) {
@ -1255,7 +1277,7 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
$success = false;
}
if(!$success)
$this->_validation_error = 10;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_document;
break;
case self::type_folder:
foreach($values as $value) {
@ -1264,7 +1286,7 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
$success = false;
}
if(!$success)
$this->_validation_error = 11;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_folder;
break;
case self::type_user:
foreach($values as $value) {
@ -1273,7 +1295,7 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
$success = false;
}
if(!$success)
$this->_validation_error = 12;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_user;
break;
case self::type_group:
foreach($values as $value) {
@ -1282,7 +1304,7 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
$success = false;
}
if(!$success)
$this->_validation_error = 13;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_group;
break;
}
@ -1291,10 +1313,16 @@ class SeedDMS_Core_AttributeDefinition { /* {{{ */
/* Check if value is in value set */
if($valueset = $this->getValueSetAsArray()) {
foreach($values as $value) {
if(!in_array($value, $valueset)) {
$success = false;
$this->_validation_error = 10;
/* An empty value cannot be the value set */
if(!$values) {
$success = false;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_valueset;
} else {
foreach($values as $value) {
if(!in_array($value, $valueset)) {
$success = false;
$this->_validation_error = SeedDMS_Core_AttributeDefinition::val_error_valueset;
}
}
}
}