You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Web/Documentation/content/main/2-tempest-in-depth/02-validation.md
+37-1Lines changed: 37 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ final class Book
39
39
}
40
40
```
41
41
42
-
If validation fails, `$failingRules` will contain a list of fields and their respective failed rules.
42
+
If validation fails, `validateValuesForClass()` returns a list of fields and their respective failed rules.
43
43
44
44
### Adding more rules
45
45
@@ -93,4 +93,40 @@ $validator->validateValues([
93
93
]);
94
94
```
95
95
96
+
If validation fails, `validateValues()` returns a list of fields and their respective failing rules.
97
+
96
98
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/packages/validation/src/Rules).
99
+
100
+
## Validating a single value
101
+
102
+
You may validate a single value against a set of rules using the `validateValue` method.
Alternatively, you may provide a closure for validation. The closure should return `true` if validation passes, or `false` otherwise. You may also return a string to specify the validation failure message.
109
+
110
+
```php
111
+
$validator->validateValue('[email protected]', function (mixed $value) {
112
+
return str_contains($value, '@');
113
+
});
114
+
```
115
+
116
+
## Accessing error messages
117
+
118
+
When validation fails, a list of fields and their respective failing rules is returned. You may call the `message` method on any rule to get a validation message.
119
+
120
+
```php
121
+
use Tempest\Support\Arr;
122
+
123
+
// Validate some value
124
+
$failures = $validator->validateValue('[email protected]', new Email());
Note that we expect to improve the way validation messages work in the future. See [this conversation](https://discord.com/channels/1236153076688359495/1294321824498323547/1294321824498323547) on our [Discord server](https://tempestphp.com/discord).
0 commit comments