Skip to content

Commit 45ca709

Browse files
committed
feat: document more validation features
1 parent 395466c commit 45ca709

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/Web/Documentation/content/main/2-tempest-in-depth/02-validation.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class Book
3939
}
4040
```
4141

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.
4343

4444
### Adding more rules
4545

@@ -93,4 +93,40 @@ $validator->validateValues([
9393
]);
9494
```
9595

96+
If validation fails, `validateValues()` returns a list of fields and their respective failing rules.
97+
9698
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.
103+
104+
```php
105+
$validator->validateValue('[email protected]', [new Email()]);
106+
```
107+
108+
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());
125+
126+
// Map failures to their message
127+
$errors = Arr\map($failures, fn (Rule $failure) => $failure->message());
128+
```
129+
130+
:::info
131+
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).
132+
:::

0 commit comments

Comments
 (0)