-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateArray.php
More file actions
79 lines (71 loc) · 2.38 KB
/
Copy pathValidateArray.php
File metadata and controls
79 lines (71 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Quatrevieux\Form\Validator\Constraint;
use Attribute;
use Quatrevieux\Form\RegistryInterface;
use Quatrevieux\Form\Validator\FieldError;
/**
* Constraint for array validation
* Will apply validation to each element of the array
*
* Note: If the value is not an array, the validation will be skipped
*
* Example:
* <code>
* class MyRequest
* {
* #[ValidateArray(constraints: [
* new Length(min: 3, max: 10),
* new Regex(pattern: '/^[a-z]+$/'),
* ])]
* public ?array $values;
* }
* </code>
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class ValidateArray implements ConstraintInterface
{
public const CODE = '1bfd08ad-82cf-57d0-a114-e9921e80986a';
public function __construct(
/**
* List of constraints to apply to each element of the array
*
* @var non-empty-list<ConstraintInterface>
*/
public array $constraints,
/**
* Global error message to show if at least one element of the array is invalid
* This message will be used only if {@see ValidateArray::$aggregateErrors} is true
*
* Use {{ item_errors }} as placeholder for the list of item errors
*
* @var string
*/
public string $message = 'Some values are invalid :' . PHP_EOL . '{{ item_errors }}',
/**
* Error message format for each item error
*
* Use as placeholders:
* - {{ key }} for the item array key. If the array is not associative, the key will be the item index. Be aware that the key value is not escaped.
* - {{ error }} for the item error message.
*
* @var string
*/
public string $itemMessage = '- On item {{ key }}: {{ error }}' . PHP_EOL,
/**
* Aggregate all item errors in a single {@see FieldError}
* The error message will be the {@see ValidateArray::$message} with the item errors concatenated
*
* If this option is false, the validation will return a {@see FieldError} for each invalid item
*
* @var bool
*/
public bool $aggregateErrors = false,
) {}
/**
* {@inheritdoc}
*/
public function getValidator(RegistryInterface $registry): ConstraintValidatorInterface
{
return new ValidateArrayValidator($registry);
}
}