← All changes
|
backend/src/Services/Field/FieldDuplicateValidationService.php
+30
-3
1.1
→
1.5
View file →
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Services\Field; |
| 4 | 4 | |
| 5 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 5 | 6 | use IvyForms\Entity\Field\Field; |
| 6 | 7 | use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface; |
| 7 | 8 | |
| 8 | 9 | /** |
| @@ -25,9 +26,10 @@ | ||
| 25 | 26 | public function checkDuplicateFieldValues( |
| 26 | 27 | array $formFields, |
| 27 | 28 | array $submissionData, |
| 28 | 29 | int $formId, |
| 29 | - ?string $pageId = null | |
| 30 | + ?string $pageId = null, | |
| 31 | + ?int $excludeEntryId = null | |
| 30 | 32 | ): array { |
| 31 | 33 | $duplicateErrors = []; |
| 32 | 34 | $isDuplicate = false; |
| 33 | 35 | $fieldValues = []; |
| @@ -43,10 +45,35 @@ | ||
| 43 | 45 | } |
| 44 | 46 | if (!$this->shouldCheckDuplicateValue($field, $fieldValues, $fidStr)) { |
| 45 | 47 | continue; |
| 46 | 48 | } |
| 47 | - $val = $fieldValues[$fidStr]; | |
| 48 | - if ($this->entryFieldRepository->checkDuplicateValue($formId, $fid, $val)) { | |
| 49 | + $rawVal = $fieldValues[$fidStr]; | |
| 50 | + $baseNormalized = Sanitizer::normalizeFieldValue($rawVal); | |
| 51 | + $val = apply_filters( | |
| 52 | + 'ivyforms/field/normalize_value_for_duplicate_check', | |
| 53 | + $baseNormalized, | |
| 54 | + $field, | |
| 55 | + $rawVal | |
| 56 | + ); | |
| 57 | + | |
| 58 | + $isFieldDuplicate = $val !== $baseNormalized | |
| 59 | + ? $this->entryFieldRepository->checkDuplicateValueNormalized( | |
| 60 | + $formId, | |
| 61 | + $fid, | |
| 62 | + (string) $val, | |
| 63 | + static function (string $stored) use ($field): string { | |
| 64 | + return (string) apply_filters( | |
| 65 | + 'ivyforms/field/normalize_value_for_duplicate_check', | |
| 66 | + Sanitizer::normalizeFieldValue($stored), | |
| 67 | + $field, | |
| 68 | + $stored | |
| 69 | + ); | |
| 70 | + }, | |
| 71 | + $excludeEntryId | |
| 72 | + ) | |
| 73 | + : $this->entryFieldRepository->checkDuplicateValue($formId, $fid, (string) $val, $excludeEntryId); | |
| 74 | + | |
| 75 | + if ($isFieldDuplicate) { | |
| 49 | 76 | $duplicateErrors[$fidStr] = true; |
| 50 | 77 | $isDuplicate = true; |
| 51 | 78 | } |
| 52 | 79 | } |