int range validation
This commit is contained in:
@@ -29,16 +29,54 @@
|
||||
|
||||
namespace Espo\Classes\FieldValidators;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Espo\ORM\Defs;
|
||||
use Espo\ORM\Entity;
|
||||
use stdClass;
|
||||
|
||||
class IntType
|
||||
{
|
||||
public function __construct(
|
||||
private Defs $defs,
|
||||
) {}
|
||||
|
||||
public function checkRequired(Entity $entity, string $field): bool
|
||||
{
|
||||
return $this->isNotEmpty($entity, $field);
|
||||
}
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
public function checkRangeInternal(Entity $entity, string $field): bool
|
||||
{
|
||||
$value = $entity->get($field);
|
||||
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$dbType = $this->defs
|
||||
->getEntity($entity->getEntityType())
|
||||
->tryGetAttribute($field)
|
||||
?->getParam('dbType') ?? Types::INTEGER;
|
||||
|
||||
$ranges = [
|
||||
Types::INTEGER => [-2147483648, 2147483647],
|
||||
Types::SMALLINT => [-32768, 32767],
|
||||
];
|
||||
|
||||
$range = $ranges[$dbType] ?? null;
|
||||
|
||||
if (!$range) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($value < $range[0] || $value > $range[1]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $validationValue
|
||||
* @noinspection PhpUnused
|
||||
|
||||
@@ -40,7 +40,8 @@
|
||||
"max"
|
||||
],
|
||||
"mandatoryValidationList": [
|
||||
"valid"
|
||||
"valid",
|
||||
"rangeInternal"
|
||||
],
|
||||
"filter": true,
|
||||
"textFilter": true,
|
||||
|
||||
Reference in New Issue
Block a user