field validation additions

This commit is contained in:
yuri
2019-02-06 16:37:53 +02:00
parent 7154fa0f4a
commit ee52b8d4d9
31 changed files with 529 additions and 31 deletions
@@ -29,9 +29,6 @@
namespace Espo\Core\FieldValidators;
use \Espo\ORM\Entity;
class CurrencyType extends FloatType
{
}
@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class DateType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
return $entity->has($field) && $entity->get($field) !== null;
}
}
@@ -0,0 +1,40 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class DatetimeOptionalType extends DatetimeType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if ($entity->has($field) && $entity->get($field) !== null) return true;
if ($entity->has($field . 'Date') && $entity->get($field . 'Date') !== null) return true;
return false;
}
}
@@ -0,0 +1,34 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class DatetimeType extends DateType
{
}
@@ -0,0 +1,70 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class EmailType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) return true;
$dataList = $entity->get($field . 'Data');
if (!is_array($dataList)) return false;
foreach ($dataList as $item) {
if (!empty($item->emailAddress)) return true;
}
return false;
}
public function checkEmailAddress(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) {
$address = $entity->get($field);
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
return false;
}
}
$dataList = $entity->get($field . 'Data');
if (is_array($dataList)) {
foreach ($dataList as $item) {
if (empty($item->emailAddress)) continue;
$address = $item->emailAddress;
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
return false;
}
}
}
return true;
}
}
@@ -0,0 +1,34 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class FileType extends LinkType
{
}
@@ -29,9 +29,6 @@
namespace Espo\Core\FieldValidators;
use \Espo\ORM\Entity;
class FloatType extends IntType
{
}
@@ -0,0 +1,34 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class ImageType extends FileType
{
}
@@ -29,12 +29,24 @@
namespace Espo\Core\FieldValidators;
use \Espo\ORM\Entity;
class IntType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
return $entity->has($field) && $entity->get($field) !== null;
}
public function checkMax(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if (!$entity->has($field) || $entity->get($field) === null) return true;
if ($entity->get($field) > $validationValue) return false;
return true;
}
public function checkMin(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if (!$entity->has($field) || $entity->get($field) === null) return true;
if ($entity->get($field) < $validationValue) return false;
return true;
}
}
@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class LinkMultipleType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
return count($entity->getLinkMultipleIdList($field)) > 0;
}
}
@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class LinkParentType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
$idAttribute = $field . 'Id';
$typeAttribute = $field . 'Type';
if (!$entity->has($idAttribute) || $entity->get($idAttribute) === '' || $entity->get($idAttribute) === null) {
return false;
}
if (!$entity->get($typeAttribute)) {
return false;
}
return true;
}
}
@@ -0,0 +1,44 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class LinkType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
$idAttribute = $field . 'Id';
if (!$entity->has($idAttribute)) {
return false;
}
return $entity->get($idAttribute) !== null && $entity->get($idAttribute) !== '';
}
}
@@ -29,8 +29,6 @@
namespace Espo\Core\FieldValidators;
use \Espo\ORM\Entity;
class PersonNameType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
@@ -0,0 +1,47 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Core\FieldValidators;
class PhoneType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) return true;
$dataList = $entity->get($field . 'Data');
if (!is_array($dataList)) return false;
foreach ($dataList as $item) {
if (!empty($item->phoneNumber)) return true;
}
return false;
}
}
@@ -29,12 +29,21 @@
namespace Espo\Core\FieldValidators;
use \Espo\ORM\Entity;
class VarcharType extends BaseType
{
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
return $entity->has($field) && $entity->get($field) !== '';
return $entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null;
}
public function checkMaxLength(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
{
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) {
$value = $entity->get($field);
if (mb_strlen($value) > $validationValue) {
return false;
}
}
return true;
}
}
@@ -47,10 +47,16 @@ class FieldValidatorManager
{
if (!$data) $data = (object) [];
$validationValue = $this->fieldManagerUtil->getEntityTypeFieldParam($entity->getEntityType(), $field, $type);
if (is_null($validationValue) || $validationValue === false) return true;
$fieldType = $this->fieldManagerUtil->getEntityTypeFieldParam($entity->getEntityType(), $field, 'type');
$validationValue = $this->fieldManagerUtil->getEntityTypeFieldParam($entity->getEntityType(), $field, $type);
$mandatoryValidationList = $this->metadata->get(['fields', $fieldType, 'mandatoryValidationList'], []);
if (!in_array($type, $mandatoryValidationList)) {
if (is_null($validationValue) || $validationValue === false) return true;
}
if (!array_key_exists($fieldType, $this->implHash)) {
$this->loadImpl($fieldType);
}
@@ -52,7 +52,11 @@
"importDisabled": true
}
},
"validationList": ["required"],
"validationList": [
"required",
"min",
"max"
],
"filter": true,
"personalData": true
}
@@ -61,6 +61,9 @@
"type":"bool"
}
],
"validationList": [
"required"
],
"filter": true,
"fieldDefs":{
"notNull":false
@@ -68,6 +68,9 @@
"type":"bool"
}
],
"validationList": [
"required"
],
"filter": true,
"fieldDefs":{
"notNull":false
@@ -73,6 +73,9 @@
"disabled": true
}
},
"validationList": [
"required"
],
"filter": true,
"notCreatable": true,
"fieldDefs":{
@@ -28,6 +28,13 @@
"customizationReadOnlyDisabled": true
}
},
"validationList": [
"required",
"emailAddress"
],
"mandatoryValidationList": [
"emailAddress"
],
"notCreatable": true,
"filter": true,
"fieldDefs":{
@@ -27,6 +27,9 @@
"notActualFields":[
"name"
],
"validationList": [
"required"
],
"filter": true,
"linkDefs": {
"type": "belongsTo",
@@ -26,7 +26,11 @@
"type":"bool"
}
],
"validationList": ["required"],
"validationList": [
"required",
"min",
"max"
],
"filter": true,
"fieldDefs":{
"notNull":false
@@ -28,6 +28,9 @@
"notActualFields":[
"name"
],
"validationList": [
"required"
],
"filter": true,
"linkDefs": {
"type": "belongsTo",
@@ -30,7 +30,11 @@
"type":"bool"
}
],
"validationList": ["required"],
"validationList": [
"required",
"min",
"max"
],
"filter": true,
"textFilter": true,
"textFilterForeign": true,
@@ -25,6 +25,9 @@
"notActualFields":[
"name"
],
"validationList": [
"required"
],
"filter": true,
"notCreatable": true
}
@@ -22,6 +22,9 @@
"notActualFields":[
"names"
],
"validationList": [
"required"
],
"notCreatable": true,
"notSortable": true,
"filter": true
@@ -26,6 +26,9 @@
"notActualFields":[
"name"
],
"validationList": [
"required"
],
"filter": true,
"notCreatable": true,
"fieldDefs":{
@@ -39,6 +39,9 @@
"customizationReadOnlyDisabled": true
}
},
"validationList": [
"required"
],
"notCreatable": true,
"filter": true,
"fieldDefs":{
@@ -31,7 +31,10 @@
"type":"bool"
}
],
"validationList": ["required"],
"validationList": [
"required",
"maxLength"
],
"filter": true,
"personalData": true,
"textFilter": true,
+13 -11
View File
@@ -507,25 +507,27 @@ class Record extends \Espo\Core\Services\Base
if (!$entity->isNew()) {
if (!$this->isFieldSetInData($data, $field)) continue;
}
$this->processDataValidationField($entity, $field, $data);
$this->processValidationField($entity, $field, $data);
}
}
protected function processDataValidationField(Entity $entity, $field, $data)
protected function processValidationField(Entity $entity, $field, $data)
{
$fieldType = $this->getFieldManagerUtil()->getEntityTypeFieldParam($this->entityType, $field, 'type');
if (!$fieldType) return;
$validationList = $this->getMetadata()->get(['fields', $fieldType, 'validationList'], []);
$mandatoryValidationList = $this->getMetadata()->get(['fields', $fieldType, 'mandatoryValidationList'], []);
$fieldValidatorManager = $this->getInjection('container')->get('fieldValidatorManager');
$validationTypeList = $this->getMetadata()->get(['fields', $fieldType, 'validationList'], []);
foreach ($validationTypeList as $validationType) {
$validationValue = $this->getFieldManagerUtil()->getEntityTypeFieldParam($this->entityType, $field, $validationType);
if (is_null($validationValue)) continue;
if (!$fieldValidatorManager->check($entity, $field, $validationType, $validationValue, $data)) {
throw new BadRequest("Validation {$validationType}: {$field}.");
foreach ($validationList as $type) {
$value = $this->getFieldManagerUtil()->getEntityTypeFieldParam($this->entityType, $field, $type);
if (is_null($value)) {
if (!in_array($type, $mandatoryValidationList)) {
continue;
}
}
if (!$fieldValidatorManager->check($entity, $field, $type, $data)) {
throw new BadRequest("Not valid data. Field: '{$field}', type: {$type}.");
}
}
}