|
| 1 | +# Collection |
| 2 | + |
| 3 | +Validates each key of an `array`, or object implementing `\Traversable`, with a set of validation constraints. |
| 4 | + |
| 5 | +```php |
| 6 | +/** @var array<mixed, Validator> $fields */ |
| 7 | +Collection( |
| 8 | + array $fields, |
| 9 | + bool $allowExtraFields = false, |
| 10 | + ?string $message = null, |
| 11 | + ?string $extraFieldsMessage = null, |
| 12 | + ?string $missingFieldsMessage = null |
| 13 | +); |
| 14 | +``` |
| 15 | + |
| 16 | +## Basic Usage |
| 17 | + |
| 18 | +```php |
| 19 | +Validator::collection(fields: [ |
| 20 | + 'name' => Validator::notBlank(), |
| 21 | + 'age' => Validator::type('int')->greaterThanOrEqual(18) |
| 22 | +])->validate([ |
| 23 | + 'name' => 'Name', |
| 24 | + 'age' => 25 |
| 25 | +]); // true |
| 26 | + |
| 27 | +Validator::collection(fields: [ |
| 28 | + 'name' => Validator::notBlank(), |
| 29 | + 'age' => Validator::type('int')->greaterThanOrEqual(18) |
| 30 | +])->validate([ |
| 31 | + 'name' => '', |
| 32 | + 'age' => 25 |
| 33 | +]); // false ("name" is blank) |
| 34 | + |
| 35 | +/////////////////////////////// |
| 36 | + |
| 37 | +// by default, extra fields are not allowed |
| 38 | +Validator::collection(fields: [ |
| 39 | + 'name' => Validator::notBlank(), |
| 40 | + 'age' => Validator::type('int')->greaterThanOrEqual(18) |
| 41 | +])->validate([ |
| 42 | + 'name' => 'Name', |
| 43 | + 'age' => 25, |
| 44 | + 'email' => 'mail@example.com' |
| 45 | +]); // false ("email" field is not allowed) |
| 46 | + |
| 47 | +// to allow extra fields, set option to true |
| 48 | +Validator::collection( |
| 49 | + fields: [ |
| 50 | + 'name' => Validator::notBlank(), |
| 51 | + 'age' => Validator::type('int')->greaterThanOrEqual(18) |
| 52 | + ], |
| 53 | + allowExtraFields: true |
| 54 | +)->validate([ |
| 55 | + 'name' => 'Name', |
| 56 | + 'age' => 25, |
| 57 | + 'email' => 'mail@example.com' |
| 58 | +]); // true |
| 59 | + |
| 60 | +/////////////////////////////// |
| 61 | + |
| 62 | +// by default, missing fields are not allowed |
| 63 | +Validator::collection(fields: [ |
| 64 | + 'name' => Validator::notBlank(), |
| 65 | + 'age' => Validator::type('int')->greaterThanOrEqual(18) |
| 66 | +])->validate([ |
| 67 | + 'age' => 25 |
| 68 | +]); // false ("name" is missing) |
| 69 | + |
| 70 | +// but it is possible to use the Optional validation for optional fields |
| 71 | +Validator::collection(fields: [ |
| 72 | + 'name' => Validator::optional( |
| 73 | + Validator::notBlank() |
| 74 | + ), |
| 75 | + 'age' => Validator::type('int')->greaterThanOrEqual(18) |
| 76 | +])->validate([ |
| 77 | + 'age' => 25 |
| 78 | +]); // true ("name" is optional) |
| 79 | +``` |
| 80 | + |
| 81 | +> [!NOTE] |
| 82 | +> An `UnexpectedValueException` will be thrown when a value in the `fields` associative array is not an instance of `Validator`. |
| 83 | +
|
| 84 | +> [!NOTE] |
| 85 | +> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Traversable`. |
| 86 | +
|
| 87 | +## Options |
| 88 | + |
| 89 | +### `fields` |
| 90 | + |
| 91 | +type: `array<mixed, Validator>` `required` |
| 92 | + |
| 93 | +Associative array with a set of validation constraints for each key. |
| 94 | + |
| 95 | +### `allowExtraFields` |
| 96 | + |
| 97 | +type: `bool` default: `false` |
| 98 | + |
| 99 | +By default, it is not allowed to have fields (array keys) that are not defined in the `fields` option. |
| 100 | +If set to `true`, it will be allowed (but not validated). |
| 101 | + |
| 102 | +### `message` |
| 103 | + |
| 104 | +type: `?string` default: `{{ message }}` |
| 105 | + |
| 106 | +Message that will be shown when one of the fields is invalid. |
| 107 | + |
| 108 | +The following parameters are available: |
| 109 | + |
| 110 | +| Parameter | Description | |
| 111 | +|-----------------|---------------------------------------| |
| 112 | +| `{{ name }}` | Name of the invalid value | |
| 113 | +| `{{ field }}` | Name of the invalid field (array key) | |
| 114 | +| `{{ message }}` | The rule message of the invalid field | |
| 115 | + |
| 116 | +### `extraFieldsMessage` |
| 117 | + |
| 118 | +type: `?string` default: `The {{ field }} field is not allowed.` |
| 119 | + |
| 120 | +Message that will be shown when the input value has a field that is not defined in the `fields` option |
| 121 | +and `allowExtraFields` is set to `false`. |
| 122 | + |
| 123 | +The following parameters are available: |
| 124 | + |
| 125 | +| Parameter | Description | |
| 126 | +|-----------------|---------------------------------------| |
| 127 | +| `{{ name }}` | Name of the invalid value | |
| 128 | +| `{{ field }}` | Name of the invalid field (array key) | |
| 129 | + |
| 130 | +### `missingFieldsMessage` |
| 131 | + |
| 132 | +type: `?string` default: `The {{ field }} field is missing.` |
| 133 | + |
| 134 | +Message that will be shown when the input value *does not* have a field that is defined in the `fields` option. |
| 135 | + |
| 136 | +The following parameters are available: |
| 137 | + |
| 138 | +| Parameter | Description | |
| 139 | +|-----------------|---------------------------------------| |
| 140 | +| `{{ name }}` | Name of the invalid value | |
| 141 | +| `{{ field }}` | Name of the invalid field (array key) | |
| 142 | + |
| 143 | +## Changelog |
| 144 | + |
| 145 | +- `1.0.0` Created |
0 commit comments