1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Core\Domain\Exception; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
class ValidationException extends RuntimeException |
8
|
|
|
{ |
9
|
|
|
/** @var array<string, array<int, string>> */ |
10
|
|
|
public readonly array $validationErrors; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param array<string, array<string, string>> $validationErrors |
14
|
|
|
* @param string $message |
15
|
|
|
*/ |
16
|
6 |
|
public function __construct(array $validationErrors, string $message = 'Validation error') |
17
|
|
|
{ |
18
|
6 |
|
parent::__construct($message); |
19
|
|
|
|
20
|
6 |
|
$this->validationErrors = $this->transformCakephpValidationErrorsToOutputFormat($validationErrors); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Transform the validation error output from the library to array that is used by the frontend. |
25
|
|
|
* Changes are small, but the main purpose is to add an abstraction layer in case the validation |
26
|
|
|
* library changes its error output format in the future so that only this function has to be |
27
|
|
|
* changed instead of the frontend. |
28
|
|
|
* |
29
|
|
|
* In cakephp/validation 5 the error array is output in the following format: |
30
|
|
|
* $cakeValidationErrors = [ |
31
|
|
|
* 'field_name' => [ |
32
|
|
|
* 'validation_rule_name' => 'Validation error message for that field', |
33
|
|
|
* 'other_validation_rule_name' => 'Another validation error message for that field', |
34
|
|
|
* ], |
35
|
|
|
* 'email' => [ |
36
|
|
|
* '_required' => 'This field is required', |
37
|
|
|
* ], |
38
|
|
|
* 'first_name' => [ |
39
|
|
|
* 'minLength' => 'Minimum length is 3', |
40
|
|
|
* ], |
41
|
|
|
* ] |
42
|
|
|
* |
43
|
|
|
* This function transforms this into the format that is used by the frontend |
44
|
|
|
* (which is roughly the same except we don't need the infringed rule name as key): |
45
|
|
|
* $outputValidationErrors = [ |
46
|
|
|
* 'field_name' => [ |
47
|
|
|
* 0 => 'Validation error message for that field', |
48
|
|
|
* 1 => 'Another validation error message for that field', |
49
|
|
|
* ], |
50
|
|
|
* 'email' => [ |
51
|
|
|
* 0 => 'This field is required', |
52
|
|
|
* ], |
53
|
|
|
* 'first_name' => [ |
54
|
|
|
* 0 => 'Minimum length is 3', |
55
|
|
|
* ], |
56
|
|
|
* ] |
57
|
|
|
* |
58
|
|
|
* @param array<string, array<string, string>> $validationErrors The cakephp validation errors |
59
|
|
|
* |
60
|
|
|
* @return array<string, array<int, string>> the transformed result in the format documented above |
61
|
|
|
*/ |
62
|
6 |
|
private function transformCakephpValidationErrorsToOutputFormat(array $validationErrors): array |
63
|
|
|
{ |
64
|
6 |
|
$validationErrorsForOutput = []; |
65
|
6 |
|
foreach ($validationErrors as $fieldName => $fieldErrors) { |
66
|
|
|
// There may be cases with multiple error messages for a single field. |
67
|
6 |
|
foreach ($fieldErrors as $infringedRuleName => $infringedRuleMessage) { |
68
|
|
|
// Output is basically the same except without the rule name as a key. |
69
|
6 |
|
$validationErrorsForOutput[$fieldName][] = $infringedRuleMessage; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
return $validationErrorsForOutput; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|