default varchar max length validation

This commit is contained in:
Yuri Kuznetsov
2022-07-03 16:11:08 +03:00
parent 6338fe68c9
commit 88db4a5020
6 changed files with 80 additions and 8 deletions
@@ -0,0 +1,63 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldValidators;
use Espo\ORM\Entity;
class TextType
{
public function checkRequired(Entity $entity, string $field): bool
{
return $this->isNotEmpty($entity, $field);
}
public function checkMaxLength(Entity $entity, string $field, int $validationValue): bool
{
if (!$this->isNotEmpty($entity, $field)) {
return true;
}
$value = $entity->get($field);
if (mb_strlen($value) > $validationValue) {
return false;
}
return true;
}
protected function isNotEmpty(Entity $entity, string $field): bool
{
return
$entity->has($field) &&
$entity->get($field) !== '' &&
$entity->get($field) !== null;
}
}
@@ -49,7 +49,7 @@ class UrlType
return $this->varcharType->checkRequired($entity, $field);
}
public function checkMaxLength(Entity $entity, string $field, int $validationValue): bool
public function checkMaxLength(Entity $entity, string $field, ?int $validationValue): bool
{
return $this->varcharType->checkMaxLength($entity, $field, $validationValue);
}
@@ -36,6 +36,8 @@ class VarcharType
{
private Metadata $metadata;
private const DEFAULT_MAX_LENGTH = 255;
public function __construct(Metadata $metadata)
{
$this->metadata = $metadata;
@@ -46,14 +48,18 @@ class VarcharType
return $this->isNotEmpty($entity, $field);
}
public function checkMaxLength(Entity $entity, string $field, int $validationValue): bool
public function checkMaxLength(Entity $entity, string $field, ?int $validationValue): bool
{
if ($this->isNotEmpty($entity, $field)) {
$value = $entity->get($field);
if (!$this->isNotEmpty($entity, $field)) {
return true;
}
if (mb_strlen($value) > $validationValue) {
return false;
}
$value = $entity->get($field);
$maxLength = $validationValue ?? self::DEFAULT_MAX_LENGTH;
if (mb_strlen($value) > $maxLength) {
return false;
}
return true;
@@ -50,7 +50,6 @@
"required",
"maxLength"
],
"validatorClassName": "Espo\\Classes\\FieldValidators\\VarcharType",
"filter": true,
"personalData": true,
"textFilter": true,
@@ -33,6 +33,7 @@
"valid"
],
"mandatoryValidationList": [
"maxLength",
"valid"
],
"filter": true,
@@ -52,6 +52,9 @@
"maxLength",
"pattern"
],
"mandatoryValidationList": [
"maxLength"
],
"filter": true,
"personalData": true,
"textFilter": true,