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: docs/2-features/03-validation.md
+37-22Lines changed: 37 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,10 @@ While validation and [data mapping](./01-mapper) often work together, the two ar
11
11
12
12
## Validating against objects
13
13
14
-
When you have raw data and an associated model or data transfer object, you may use the `validateValuesForClass` method on the {b`\Tempest\Validation\Validator`}.
14
+
When you have raw data and an associated model or data transfer object, you may use the `validateValuesForClass()` method on the {b`\Tempest\Validation\Validator`}. Note that the validator needs to be [resolved from the container](../1-essentials/05-container.md#injecting-dependencies).
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/packages/validation/src/Rules).
63
+
:::
65
64
66
65
### Skipping validation
67
66
@@ -79,54 +78,70 @@ final class Book
79
78
80
79
## Validating against specific rules
81
80
82
-
If you don't have a model or data transfer object to validate data against, you may alternatively use the `validateValues` and provide an array of rules.
81
+
If you don't have a model or data transfer object to validate data against, you may alternatively use the `validateValues()` and provide an array of rules.
If validation fails, `validateValues()` returns a list of fields and their respective failing rules.
97
96
97
+
:::info
98
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
+
:::
99
100
100
101
## Validating a single value
101
102
102
-
You may validate a single value against a set of rules using the `validateValue` method.
103
+
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
110
111
```php
111
-
$validator->validateValue('[email protected]', function (mixed $value) {
112
+
$this->validator->validateValue('[email protected]', function (mixed $value) {
112
113
return str_contains($value, '@');
113
114
});
114
115
```
115
116
116
117
## Accessing error messages
117
118
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
+
When validation fails, a list of fields and their respective failing rules is returned. You may call the `getErrorMessage` method on the validator to get a[localized](./11-localization.md) validation message.
119
120
120
121
```php
121
122
use Tempest\Support\Arr;
122
123
123
124
// Validate some value
124
-
$failures = $validator->validateValue('[email protected]', new Email());
125
+
$failures = $this->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).
132
-
:::
131
+
You may also specify the field name of the validation failure to get a localized message for that field.
You may override the default validation messages by adding a [translation file](../2-features/11-localization.md#defining-translation-messages) anywhere in your codebase. Note that Tempest uses the [MessageFormat 2.0](https://messageformat.unicode.org/) format for localization.
0 commit comments