Skip to content

Commit 19d4c22

Browse files
committed
Add section working with error messages
1 parent 175eadb commit 19d4c22

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,123 @@ $validation_a = $validator->make($dataset_a, [
258258
$validation_a->validate();
259259
```
260260

261+
## Working with Error Message
262+
263+
Errors messages are collected in `Rakit\Validation\ErrorBag` object that you can get it using `errors()` method.
264+
265+
```php
266+
$validation = $validator->validate($inputs, $rules);
267+
268+
$errors = $validation->errors(); // << ErrorBag
269+
```
270+
271+
Now you can use methods below to retrieves errors messages:
272+
273+
#### `all(string $format = ':message')`
274+
275+
Get all messages as flatten array.
276+
277+
Examples:
278+
279+
```php
280+
$messages = $errors->all();
281+
// [
282+
// 'Email is not valid email',
283+
// 'Password minimum 6 character',
284+
// 'Password must contains capital letters'
285+
// ]
286+
287+
$messages = $errors->all('<li>:message</li>');
288+
// [
289+
// '<li>Email is not valid email</li>',
290+
// '<li>Password minimum 6 character</li>',
291+
// '<li>Password must contains capital letters</li>'
292+
// ]
293+
```
294+
295+
#### `firstOfAll(string $format = ':message', bool $dotNotation = false)`
296+
297+
Get only first message from all existing keys.
298+
299+
Examples:
300+
301+
```php
302+
$messages = $errors->firstOfAll();
303+
// [
304+
// 'email' => Email is not valid email',
305+
// 'password' => 'Password minimum 6 character',
306+
// ]
307+
308+
$messages = $errors->firstOfAll('<li>:message</li>');
309+
// [
310+
// 'email' => '<li>Email is not valid email</li>',
311+
// 'password' => '<li>Password minimum 6 character</li>',
312+
// ]
313+
```
314+
315+
Argument `$dotNotation` is for array validation.
316+
If it is `false` it will return original array structure, if it `true` it will return flatten array with dot notation keys.
317+
318+
For example:
319+
320+
```php
321+
$messages = $errors->firstOfAll(':message', false);
322+
// [
323+
// 'contacts' => [
324+
// 1 => [
325+
// 'email' => 'Email is not valid email',
326+
// 'phone' => 'Phone is not valid phone number'
327+
// ],
328+
// ],
329+
// ]
330+
331+
$messages = $errors->firstOfAll(':message', true);
332+
// [
333+
// 'contacts.1.email' => 'Email is not valid email',
334+
// 'contacts.1.phone' => 'Email is not valid phone number',
335+
// ]
336+
```
337+
338+
#### `first(string $key)`
339+
340+
Get first message from given key. It will return `string` if key has any error message, or `null` if key has no errors.
341+
342+
For example:
343+
344+
```php
345+
if ($emailError = $errors->first('email')) {
346+
echo $emailError;
347+
}
348+
```
349+
350+
#### `toArray()`
351+
352+
Get all messages grouped by it's keys.
353+
354+
For example:
355+
356+
```php
357+
$messages = $errors->toArray();
358+
// [
359+
// 'email' => [
360+
// 'Email is not valid email'
361+
// ],
362+
// 'password' => [
363+
// 'Password minimum 6 character',
364+
// 'Password must contains capital letters'
365+
// ]
366+
// ]
367+
```
368+
369+
#### `count()`
370+
371+
Get count messages.
372+
373+
#### `has(string $key)`
374+
375+
Check if given key has an error. It returns `bool` if a key has an error, and otherwise.
376+
377+
261378
## Available Rules
262379

263380
> Click to show details.

0 commit comments

Comments
 (0)