Skip to content

EspoCRM – Open Source CRM Application

validationDependsOnFieldList

Yurii27546e34b5d7
diff --git a/application/Espo/Core/FieldValidation/FieldValidationManager.php b/application/Espo/Core/FieldValidation/FieldValidationManager.php
index 4a76d44a44..473dd91844 100644
--- a/application/Espo/Core/FieldValidation/FieldValidationManager.php
+++ b/application/Espo/Core/FieldValidation/FieldValidationManager.php
@@ -122,11 +122,13 @@ class FieldValidationManager
foreach ($fieldList as $field) {
$typeList = null;
+ $fieldDefs = $entityDefs->tryGetField($field);
+
if (
!$entity->isNew() &&
$dataIsSet &&
- !$entityDefs->tryGetField($field)?->getParam('forceValidation') &&
- !$this->isFieldSetInData($entityType, $field, $data)
+ !$fieldDefs?->getParam('forceValidation') &&
+ !$this->isFieldSetInData($entityType, $field, $data, $fieldDefs)
) {
if (!$this->hasRequiredLogic($entityType, $field)) {
continue;
@@ -427,10 +429,19 @@ class FieldValidationManager
$this->checkerCache[$key] = $this->checkerFactory->create($entityType, $field);
}
- private function isFieldSetInData(string $entityType, string $field, stdClass $data): bool
+ private function isFieldSetInData(string $entityType, string $field, stdClass $data, ?Defs\FieldDefs $defs): bool
{
$attributeList = $this->fieldUtil->getActualAttributeList($entityType, $field);
+ $additionalFieldList = $defs?->getParam('validationDependsOnFieldList') ?? [];
+
+ foreach ($additionalFieldList as $additionalField) {
+ $attributeList = array_merge(
+ $attributeList,
+ $this->fieldUtil->getActualAttributeList($entityType, $additionalField)
+ );
+ }
+
$isSet = false;
foreach ($attributeList as $attribute) {
diff --git a/schema/metadata/entityDefs.json b/schema/metadata/entityDefs.json
index c2065070a2..729946c2a6 100644
--- a/schema/metadata/entityDefs.json
+++ b/schema/metadata/entityDefs.json
@@ -1027,6 +1027,13 @@
"type": "boolean",
"description": "Always to perform validation, regardless whether field values are sent. As of v8.2."
},
+ "validationDependsOnFieldList": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "description": "Change of any of the listed fields triggers the validation. As of v9.4."
+ },
"validatorClassNameMap": {
"type": "object",
"description": "Validators defined as validation-type => class-name map. Classes should implement Espo\\Core\\FieldValidation\\Validator interface. If not defined for a type, metadata > fields > {fieldType} > validatorClassNameMap > {type} is used.",
diff --git a/tests/integration/Espo/Record/FieldValidationTest.php b/tests/integration/Espo/Record/FieldValidationTest.php
index 9ffaf356af..498355a677 100644
--- a/tests/integration/Espo/Record/FieldValidationTest.php
+++ b/tests/integration/Espo/Record/FieldValidationTest.php
@@ -717,4 +717,39 @@ class FieldValidationTest extends BaseTestCase
}
$this->assertFalse($isThrown);
}
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testDependsOn(): void
+ {
+ $app = $this->createApplication();
+
+ $this->setApplication($app);
+
+ $em = $this->getEntityManager();
+
+ $account = $em->createEntity('Account', [
+ 'name' => 'Test',
+ ]);
+
+ $this->setFieldsDefs($app, 'Account', [
+ 'description' => [
+ 'required' => true,
+ 'validationDependsOnFieldList' => ['name'],
+ ],
+ ]);
+
+ $this->setApplication($app);
+ $this->reCreateApplication();
+
+ $this->expectException(BadRequest::class);
+
+ $this->getContainer()
+ ->getByClass(ServiceContainer::class)
+ ->get('Account')
+ ->update($account->getId(), (object) [
+ 'name' => 'Test 1',
+ ], UpdateParams::create());
+ }
}