PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Field / FieldDuplicateValidationService.php

FieldDuplicateValidationService.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/Services/Field/FieldDuplicateValidationService.php

79 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Field;
4
5 use IvyForms\Entity\Field\Field;
6 use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface;
7
8 /**
9 * Validates no-duplicates field values against stored submissions.
10 */
11 class FieldDuplicateValidationService
12 {
13 private EntryFieldRepositoryInterface $entryFieldRepository;
14
15 public function __construct(EntryFieldRepositoryInterface $entryFieldRepository)
16 {
17 $this->entryFieldRepository = $entryFieldRepository;
18 }
19
20 /**
21 * @param Field[] $formFields
22 * @param array<string,mixed> $submissionData
23 * @return array{0: bool, 1: array<int|string,bool>}
24 */
25 public function checkDuplicateFieldValues(
26 array $formFields,
27 array $submissionData,
28 int $formId,
29 ?string $pageId = null
30 ): array {
31 $duplicateErrors = [];
32 $isDuplicate = false;
33 $fieldValues = [];
34
35 foreach ($formFields as $field) {
36 if (!$this->isFieldOnPage($field, $pageId)) {
37 continue;
38 }
39 $fid = $field->getId();
40 $fidStr = (string) $fid;
41 if (isset($submissionData[$fidStr])) {
42 $fieldValues[$fidStr] = $submissionData[$fidStr];
43 }
44 if (!$this->shouldCheckDuplicateValue($field, $fieldValues, $fidStr)) {
45 continue;
46 }
47 $val = $fieldValues[$fidStr];
48 if ($this->entryFieldRepository->checkDuplicateValue($formId, $fid, $val)) {
49 $duplicateErrors[$fidStr] = true;
50 $isDuplicate = true;
51 }
52 }
53
54 return [$isDuplicate, $duplicateErrors];
55 }
56
57 private function isFieldOnPage(Field $field, ?string $pageId): bool
58 {
59 return $pageId === null || $field->getPageId() === $pageId;
60 }
61
62 /**
63 * @param array<string, mixed> $fieldValues
64 */
65 private function shouldCheckDuplicateValue(Field $field, array $fieldValues, string $fidStr): bool
66 {
67 if (!$field->getFieldAdvancedSettings()->isNoDuplicates()) {
68 return false;
69 }
70 if (!array_key_exists($fidStr, $fieldValues)) {
71 return false;
72 }
73
74 $value = $fieldValues[$fidStr];
75
76 return $value !== '' && $value !== null;
77 }
78 }
79