Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 42178c6

Browse files
committed
feat: added Choice rule documentation
1 parent 7619714 commit 42178c6

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

docs/03-rules-all.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## All
22

3-
Validates every element of an `array` with a set of rules.
3+
Validates every element of an `array` with a given set of rules.
44

55
## Basic Usage
66

docs/03-rules-choice.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Choice
2+
3+
Validates that a value (or multiple values) exist in a given set of choices.
4+
5+
```php
6+
Choice(
7+
array $constraints,
8+
bool $multiple = false,
9+
?int $minConstraint = null,
10+
?int $maxConstraint = null,
11+
string $message = 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".';
12+
string $multipleMessage = 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".';
13+
string $minMessage = 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.';
14+
string $maxMessage = 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.';
15+
);
16+
```
17+
18+
## Basic Usage
19+
20+
```php
21+
// Single choice
22+
Validator::choice(['red', 'green', 'blue'])->validate('green'); // true
23+
Validator::choice(['red', 'green', 'blue'])->validate('yellow'); // false
24+
25+
// Multiple choices
26+
Validator::choice(['red', 'green', 'blue'], multiple: true)->validate(['red', 'blue']); // true;
27+
Validator::choice(['red', 'green', 'blue'], multiple: true)->validate(['red', 'yellow']); // false;
28+
29+
// Multiple with minimum number of choices
30+
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2)->validate(['red', 'blue']); // true
31+
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2)->validate(['red']); // false
32+
33+
// Multiple with maximum number of choices
34+
Validator::choice(['red', 'green', 'blue'], multiple: true, maxConstraint: 2)->validate(['red', 'blue']); // true
35+
Validator::choice(['red', 'green', 'blue'], multiple: true, maxConstraint: 2)->validate(['red', 'green', 'blue']); // false
36+
37+
// Multiple with minimum and maximum number of choices
38+
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2, maxConstraint: 3)->validate(['red', 'blue']); // true
39+
Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2, maxConstraint: 3)->validate(['red']); // false
40+
```
41+
42+
> **Note**
43+
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and value to be validated is not an `array`.
44+
45+
> **Note**
46+
> An `UnexpectedValueException` will be thrown when the `minConstraint` value is greater than or equal to the `maxConstraint` value.
47+
48+
## Options
49+
50+
### `constraints`
51+
52+
type: `array` `required`
53+
54+
Collection of choices to be validated against the input value.
55+
56+
### `multiple`
57+
58+
type: `bool` default: `false`
59+
60+
If this option is `true`, validation against an `array` of input values is enabled.
61+
Each element of the input array must be a valid choice, otherwise it will fail.
62+
63+
### `minConstraint`
64+
65+
type: `?int` default: `null`
66+
67+
If `multiple` is `true`, set a minimum number of input values to be required.
68+
69+
For example, if `minConstraint` is 2, the input array must have at least 2 values.
70+
71+
### `maxConstraint`
72+
73+
type: `?int` default: `null`
74+
75+
If `multiple` is `true`, set a maximum number of input values to be required.
76+
77+
For example, if `maxConstraint` is 2, the input array must have at most 2 values.
78+
79+
### `message`
80+
81+
type `string` default: `The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".`
82+
83+
Message that will be shown if input value is not a valid choice.
84+
85+
The following parameters are available:
86+
87+
| Parameter | Description |
88+
|---------------------|-----------------------------------|
89+
| `{{ value }}` | The current invalid value |
90+
| `{{ name }}` | Name of the value being validated |
91+
| `{{ constraints }}` | The array of valid choices |
92+
93+
### `multipleMessage`
94+
95+
type `string` default: `The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".`
96+
97+
Message that will be shown when `multiple` is `true` and at least one of the input array values is not a valid choice.
98+
99+
The following parameters are available:
100+
101+
| Parameter | Description |
102+
|---------------------|-----------------------------------|
103+
| `{{ value }}` | The current invalid value |
104+
| `{{ name }}` | Name of the value being validated |
105+
| `{{ constraints }}` | The array of valid choices |
106+
107+
### `minMessage`
108+
109+
type `string` default: `The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
110+
111+
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `minConstraint`.
112+
113+
The following parameters are available:
114+
115+
| Parameter | Description |
116+
|-----------------------|--------------------------------------|
117+
| `{{ value }}` | The current invalid value |
118+
| `{{ numValues }}` | The current invalid number of values |
119+
| `{{ name }}` | Name of the value being validated |
120+
| `{{ constraints }}` | The array of valid choices |
121+
| `{{ minConstraint }}` | The minimum number of valid choices |
122+
| `{{ maxConstraint }}` | The maximum number of valid choices |
123+
124+
### `maxMessage`
125+
126+
type `string` default: `The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
127+
128+
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `maxConstraint`.
129+
130+
The following parameters are available:
131+
132+
| Parameter | Description |
133+
|-----------------------|--------------------------------------|
134+
| `{{ value }}` | The current invalid value |
135+
| `{{ numValues }}` | The current invalid number of values |
136+
| `{{ name }}` | Name of the value being validated |
137+
| `{{ constraints }}` | The array of valid choices |
138+
| `{{ minConstraint }}` | The minimum number of valid choices |
139+
| `{{ maxConstraint }}` | The maximum number of valid choices |

0 commit comments

Comments
 (0)