Skip to content

EspoCRM – Open Source CRM Application

formula: validation exception

Yuriifad05ffd058d
diff --git a/application/Espo/Core/Formula/Exceptions/ValidationFunctionException.php b/application/Espo/Core/Formula/Exceptions/ValidationFunctionException.php
new file mode 100644
index 0000000000..b816fd5bd1
--- /dev/null
+++ b/application/Espo/Core/Formula/Exceptions/ValidationFunctionException.php
@@ -0,0 +1,38 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://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 Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Formula\Exceptions;
+
+/**
+ * To be thrown by an exception function.
+ *
+ * @internal
+ */
+class ValidationFunctionException extends Error
+{}
diff --git a/application/Espo/Core/Formula/Functions/ExceptionGroup/ValidationType.php b/application/Espo/Core/Formula/Functions/ExceptionGroup/ValidationType.php
new file mode 100644
index 0000000000..d6045458a9
--- /dev/null
+++ b/application/Espo/Core/Formula/Functions/ExceptionGroup/ValidationType.php
@@ -0,0 +1,52 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://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 Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Formula\Functions\ExceptionGroup;
+
+use Espo\Core\Formula\EvaluatedArgumentList;
+use Espo\Core\Formula\Exceptions\BadArgumentValue;
+use Espo\Core\Formula\Exceptions\ValidationFunctionException;
+use Espo\Core\Formula\Func;
+
+/**
+ * @noinspection PhpUnused
+ */
+class ValidationType implements Func
+{
+ public function process(EvaluatedArgumentList $arguments): never
+ {
+ $message = $arguments[0] ?? null;
+
+ if ($message !== null && !is_string($message)) {
+ throw BadArgumentValue::create(1, 'string');
+ }
+
+ throw new ValidationFunctionException($message ?? '');
+ }
+}
diff --git a/application/Espo/Hooks/Common/Formula.php b/application/Espo/Hooks/Common/Formula.php
index ea1f7cfda1..10933b2036 100644
--- a/application/Espo/Hooks/Common/Formula.php
+++ b/application/Espo/Hooks/Common/Formula.php
@@ -29,8 +29,10 @@
namespace Espo\Hooks\Common;
+use Espo\Core\Formula\Exceptions\ValidationFunctionException;
use Espo\ORM\Entity;
use Espo\ORM\Exceptions\PersistenceException;
+use Espo\ORM\Exceptions\ValidationException;
use Espo\ORM\Repository\Option\SaveOptions;
use Espo\Core\Hook\Hook\BeforeSave;
use Espo\Core\Formula\Manager as FormulaManager;
@@ -79,6 +81,10 @@ class Formula implements BeforeSave
try {
$this->formulaManager->run($script, $entity, $variables);
} catch (Exception $e) {
+ if ($e instanceof ValidationFunctionException) {
+ throw new ValidationException("Before-save validation error.", previous: $e);
+ }
+
throw new PersistenceException("Before-save formula script failed.", previous: $e);
}
}
diff --git a/client/src/views/admin/entity-manager/record/edit-formula.js b/client/src/views/admin/entity-manager/record/edit-formula.js
index c96f396723..1b2c92b8f6 100644
--- a/client/src/views/admin/entity-manager/record/edit-formula.js
+++ b/client/src/views/admin/entity-manager/record/edit-formula.js
@@ -48,6 +48,8 @@ class EntityManagerEditFormulaRecordView extends BaseRecordView {
if (this.options.type === 'beforeSaveApiScript') {
additionalFunctionDataList = this.getRecordServiceFunctionDataList();
+ } else if (this.options.type === 'beforeSaveCustomScript') {
+ additionalFunctionDataList = this.getBeforeSaveFunctionDataList();
}
this.createField(
@@ -88,6 +90,19 @@ class EntityManagerEditFormulaRecordView extends BaseRecordView {
},
];
}
+
+ /**
+ * @private
+ * @return {Record[]}
+ */
+ getBeforeSaveFunctionDataList() {
+ return [
+ {
+ name: 'exception\\validation',
+ insertText: 'exception\\validation()',
+ },
+ ];
+ }
}
export default EntityManagerEditFormulaRecordView;