diff --git a/application/Espo/Classes/FieldValidators/IntType.php b/application/Espo/Classes/FieldValidators/IntType.php index e8f36d5025..a48a0b3edc 100644 --- a/application/Espo/Classes/FieldValidators/IntType.php +++ b/application/Espo/Classes/FieldValidators/IntType.php @@ -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 diff --git a/application/Espo/Resources/metadata/fields/int.json b/application/Espo/Resources/metadata/fields/int.json index 513c104d78..774e199289 100644 --- a/application/Espo/Resources/metadata/fields/int.json +++ b/application/Espo/Resources/metadata/fields/int.json @@ -40,7 +40,8 @@ "max" ], "mandatoryValidationList": [ - "valid" + "valid", + "rangeInternal" ], "filter": true, "textFilter": true,