Skip to content

Commit 5616544

Browse files
committed
Added documentation for using translation
1 parent 0db9279 commit 5616544

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,38 @@ $validation_a = $validator->make($dataset_a, [
267267
$validation_a->validate();
268268
```
269269

270+
## Translation
271+
272+
Translation is different with custom messages.
273+
Translation needed when you use custom message for rule `in`, `not_in`, `mimes`, and `uploadede_file`.
274+
275+
For example if you use rule `in:1,2,3` we will set invalid message like "The Attribute only allows '1', '2', or '3'"
276+
where message "'1', '2', or '3'" is comes from ":allowed_values" tag.
277+
So if you have custom Indonesian message ":attribute hanya mengizinkan :allowed_values",
278+
we will set invalid message like "Attribute hanya mengizinkan '1', '2', or '3'" which is the "or" word is not Indonesian language.
279+
280+
So, to solves this problem, we can use translation like this:
281+
282+
```php
283+
// Set translation for words 'and' and 'or'.
284+
$validator->setTranslations([
285+
'and' => 'dan',
286+
'or' => 'atau'
287+
]);
288+
289+
// Set custom message for 'in' rule
290+
$validator->setMessage('in', ":attribute hanya mengizinkan :allowed_values");
291+
292+
// Validate
293+
$validation = $validator->validate($inputs, [
294+
'nomor' => 'in:1,2,3'
295+
]);
296+
297+
$message = $validation->errors()->first('nomor'); // "Nomor hanya mengizinkan '1', '2', atau '3'"
298+
```
299+
300+
> Actually, our built-in rules only use words 'and' and 'or' that you may need to translates.
301+
270302
## Working with Error Message
271303

272304
Errors messages are collected in `Rakit\Validation\ErrorBag` object that you can get it using `errors()` method.

0 commit comments

Comments
 (0)