varchar pattern

This commit is contained in:
Yuri Kuznetsov
2022-07-02 13:12:32 +03:00
parent 452f7a5d98
commit 10597931be
41 changed files with 234 additions and 50 deletions
@@ -29,10 +29,18 @@
namespace Espo\Classes\FieldValidators;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Entity;
class VarcharType
{
private Metadata $metadata;
public function __construct(Metadata $metadata)
{
$this->metadata = $metadata;
}
public function checkRequired(Entity $entity, string $field): bool
{
return $this->isNotEmpty($entity, $field);
@@ -51,8 +59,32 @@ class VarcharType
return true;
}
public function checkPattern(Entity $entity, string $field, ?string $validationValue): bool
{
if (!$this->isNotEmpty($entity, $field) || !$validationValue) {
return true;
}
$value = $entity->get($field);
$pattern = $validationValue;
if ($validationValue[0] === '$') {
$patternName = substr($validationValue, 1);
$pattern = $this->metadata->get(['app', 'regExpPatterns', $patternName, 'pattern']) ??
$pattern;
}
$preparedPattern = '/^' . $pattern . '$/';
return (bool) preg_match($preparedPattern, $value);
}
protected function isNotEmpty(Entity $entity, string $field): bool
{
return $entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null;
return
$entity->has($field) &&
$entity->get($field) !== '' &&
$entity->get($field) !== null;
}
}
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"description": {
"type": "text"
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"description": {
"type": "text"
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"order": {
"type": "int",
@@ -106,4 +107,4 @@
}
}
}
}
}
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"description": {
"type": "text"
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -4,7 +4,8 @@
"type": "varchar",
"maxLength": 249,
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"website": {
"type": "url",
@@ -86,7 +87,8 @@
"sicCode": {
"type": "varchar",
"maxLength": 40,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"contactRole": {
"type": "varchar",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"number": {
"type": "autoincrement",
@@ -4,7 +4,8 @@
"type": "varchar",
"required": true,
"view": "crm:views/document/fields/name",
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"file": {
"type": "file",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"description": {
"type": "text"
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"description": {
"type": "text"
@@ -27,7 +27,8 @@
},
"title": {
"type": "varchar",
"maxLength": 100
"maxLength": 100,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -137,7 +138,8 @@
"view": "views/fields/user"
},
"accountName": {
"type": "varchar"
"type": "varchar",
"pattern": "$noBadCharacters"
},
"assignedUser": {
"type": "link",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"amount": {
"type": "currency",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"entryCount": {
"type": "int",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"status": {
"type": "enum",
@@ -213,7 +213,8 @@
"onlyDefaultCurrency": "Only default currency",
"displayRawText": "Display raw text (no markdown)",
"conversionDisabled": "Disable Conversion",
"decimalPlaces": "Decimal Places"
"decimalPlaces": "Decimal Places",
"pattern": "Pattern"
},
"strings" : {
"rebuildRequired": "Rebuild is required"
@@ -87,7 +87,8 @@
"fileAccept": "Which file types to accept. It's possible to add custom items.",
"barcodeLastChar": "For EAN-13 type.",
"maxFileSize": "If empty or 0 then no limit.",
"conversionDisabled": "The currency conversion action won't be applied to this field."
"conversionDisabled": "The currency conversion action won't be applied to this field.",
"pattern": "A regular expression to check a field value against."
},
"fieldParts": {
"address": {
@@ -277,6 +277,8 @@
"notModified": "You have not modified the record",
"duplicate": "The record you are creating might already exist",
"dropToAttach": "Drop to attach",
"fieldNotMatchingPattern": "{field} does not match the pattern `{pattern}`",
"fieldNotMatchingPattern$noBadCharacters": "{field} contains not allowed characters",
"fieldInvalid": "{field} is invalid",
"fieldIsRequired": "{field} is required",
"fieldShouldBeEmail": "{field} should be a valid email",
@@ -0,0 +1,5 @@
{
"noBadCharacters": {
"pattern": "[^<>=]+"
}
}
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"emailAddress": {
"type": "varchar",
@@ -5,7 +5,8 @@
"required": true,
"maxLength": 100,
"tooltip": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"from": {
"type": "varchar",
@@ -4,7 +4,8 @@
"type": "varchar",
"required": true,
"maxLength": 64,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"order": {
"type": "int"
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"emailAddress": {
"type": "varchar",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"maxLength": 100,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"logo": {
"type": "image"
@@ -4,7 +4,8 @@
"maxLength": 150,
"required": true,
"type": "varchar",
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"data": {
"type": "jsonObject"
@@ -4,7 +4,8 @@
"maxLength": 150,
"required": true,
"type": "varchar",
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"assignmentPermission": {
"type": "enum",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"maxLength": 100,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"roles": {
"type": "linkMultiple",
@@ -3,7 +3,8 @@
"name": {
"type": "varchar",
"required": true,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"body": {
"type": "wysiwyg",
@@ -125,4 +126,4 @@
"order": "asc"
},
"optimisticConcurrencyControl": true
}
}
@@ -91,7 +91,8 @@
"title": {
"type": "varchar",
"maxLength": 100,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"position": {
"type": "varchar",
@@ -17,26 +17,30 @@
"trim": true,
"maxLength": 255,
"view": "views/fields/address-city",
"customizationOptionsDisabled": true
"customizationOptionsDisabled": true,
"pattern": "$noBadCharacters"
},
"state": {
"type":"varchar",
"trim": true,
"maxLength": 255,
"view": "views/fields/address-state",
"customizationOptionsDisabled": true
"customizationOptionsDisabled": true,
"pattern": "$noBadCharacters"
},
"country": {
"type":"varchar",
"trim": true,
"maxLength": 255,
"view": "views/fields/address-country",
"customizationOptionsDisabled": true
"customizationOptionsDisabled": true,
"pattern": "$noBadCharacters"
},
"postalCode": {
"type":"varchar",
"maxLength": 40,
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"map": {
"type": "map",
@@ -21,16 +21,19 @@
},
"first":{
"type":"varchar",
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"last":{
"type":"varchar",
"trim": true
"trim": true,
"pattern": "$noBadCharacters"
},
"middle":{
"type": "varchar",
"trim": true,
"maxLength": 100
"maxLength": 100,
"pattern": "$noBadCharacters"
}
},
"naming":"prefix",
@@ -46,4 +49,4 @@
"first",
"last"
]
}
}
@@ -21,6 +21,13 @@
"name": "options",
"type": "multiEnum"
},
{
"name": "pattern",
"type": "varchar",
"default": null,
"tooltip": true,
"view": "views/admin/field-manager/fields/pattern"
},
{
"name": "audited",
"type": "bool"
@@ -37,7 +44,8 @@
],
"validationList": [
"required",
"maxLength"
"maxLength",
"pattern"
],
"filter": true,
"personalData": true,
@@ -0,0 +1,48 @@
/************************************************************************
* 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.
************************************************************************/
define('views/admin/field-manager/fields/pattern', ['views/fields/varchar'], function (Dep) {
/**
* @class
* @name Class
* @memberOf module:views/admin/field-manager/fields/pattern
* @extends module:views/fields/varchar.Class
*/
return Dep.extend(/** @lends module:views/admin/field-manager/fields/pattern.Class# */{
noSpellCheck: true,
setupOptions: function () {
let patternList = Object.keys(this.getMetadata().get(['app', 'regExpPatterns']) || {})
.map(item => '$' + item);
this.setOptionList(patternList);
},
})
});
+26 -1
View File
@@ -26,7 +26,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
define('views/fields/address', ['views/fields/base'], function (Dep) {
define('views/fields/address', ['views/fields/base', 'views/fields/varchar'], function (Dep, Varchar) {
/**
* An address field.
@@ -56,6 +56,14 @@ define('views/fields/address', ['views/fields/base'], function (Dep) {
searchTemplate: 'fields/address/search',
/**
* @inheritDoc
*/
validations: [
'required',
'pattern',
],
/**
* @inheritDoc
*/
@@ -606,6 +614,23 @@ define('views/fields/address', ['views/fields/base'], function (Dep) {
this.model.getFieldParam(this.countryField, 'required');
},
validatePattern: function () {
let fieldList = [
this.postalCodeField,
this.stateField,
this.cityField,
this.countryField,
];
let result = false;
for (let field of fieldList) {
result = Varchar.prototype.fieldValidatePattern.call(this, field) || result;
}
return result;
},
fetch: function () {
var data = {};
+26 -2
View File
@@ -28,7 +28,13 @@
define('views/fields/person-name', ['views/fields/varchar'], function (Dep) {
return Dep.extend({
/**
* @class
* @name Class
* @extends module:views/fields/varchar.Class
* @memberOf module:views/fields/person-name
*/
return Dep.extend(/** @lends module:views/fields/person-name.Class# */{
type: 'personName',
@@ -42,6 +48,14 @@ define('views/fields/person-name', ['views/fields/varchar'], function (Dep) {
editTemplateFirstMiddleLast: 'fields/person-name/edit-first-middle-last',
/**
* @inheritDoc
*/
validations: [
'required',
'pattern',
],
data: function () {
var data = Dep.prototype.data.call(this);
@@ -270,6 +284,16 @@ define('views/fields/person-name', ['views/fields/varchar'], function (Dep) {
return result;
},
validatePattern: function () {
let result = false;
result = this.fieldValidatePattern(this.firstField) || result;
result = this.fieldValidatePattern(this.lastField) || result;
result = this.fieldValidatePattern(this.middleField) || result;
return result;
},
hasRequiredMarker: function () {
if (this.isRequired()) {
return true;
@@ -281,7 +305,7 @@ define('views/fields/person-name', ['views/fields/varchar'], function (Dep) {
this.model.getFieldParam(this.lastField, 'required');
},
fetch: function (form) {
fetch: function () {
var data = {};
data[this.salutationField] = this.$salutation.val() || null;
+2 -2
View File
@@ -306,11 +306,11 @@ define('views/fields/varchar', ['views/fields/base'], function (Dep) {
let messageKey = 'fieldNotMatchingPattern';
if (pattern[0] === '$') {
let patternName = pattern;
let patternName = pattern.slice(1);
let foundPattern = this.getMetadata().get(['app', 'regExpPatterns', patternName, 'pattern']);
if (foundPattern) {
messageKey += patternName;
messageKey += '$' + patternName;
pattern = foundPattern;
}
}