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
Finally, the base `Validator` class needs to be published to a new `app/Validation` directory. This can be done using the `vendor:publish` command:
34
+
Finally, the base `Validator` class needs to be published to a new `app/Validators` directory. This can be done using the `vendor:publish` command:
35
35
36
36
`php artisan vendor:publish`
37
37
38
38
### Usage
39
39
---
40
-
All validators reside in the `app/Validation` directory and extend the abstract class `App\Validation\Validator`. There should be one validator class per model. For example, the validator for a `User` model could be called `UserValidator`.
40
+
All validators reside in the `app/Validators` directory and extend the abstract class `App\Validators\Validator`. There should be one validator class per model. For example, the validator for a `User` model could be called `UserValidator`.
41
41
42
42
Laravel Validation provides a useful artisan command for generating new validators on the fly. Let's create a validator for our `User` model and define some rules:
43
43
44
44
`php artisan make:validator UserValidator`
45
45
46
-
This will create a new `UserValidator` class in the `app/Validation` directory that looks like this:
46
+
This will create a new `UserValidator` class in the `app/Validators` directory that looks like this:
47
47
48
48
```
49
49
<?php
50
50
51
-
namespace App\Validation;
51
+
namespace App\Validators;
52
52
53
-
use App\Validation\Validator;
53
+
use App\Validators\Validator;
54
54
55
55
class UserValidator extends Validator
56
56
{
@@ -81,7 +81,7 @@ Great! We now have a validator class named `UserValidator` with the rules we int
81
81
82
82
First, we will want to import this class into our controller:
83
83
84
-
`use App\Validation\UserValidator`
84
+
`use App\Validators\UserValidator`
85
85
86
86
Now, let's validate a POST request for the controller's `store` method:
87
87
@@ -126,7 +126,7 @@ In this case, we are adding a rule for a `name` field, which will be merged with
126
126
##### Multiple Rulesets
127
127
Laravel Validation expects a `rules` property on your validator class, but it is possible to define additional properties and use those in specific cases. You may have different requirements when updating a record vs storing, or have unique rules if a user is of a specific role.
128
128
129
-
Let's define an `updating` property on the `App\Validation\UserValidator` class with specific rules for updating a user:
129
+
Let's define an `updating` property on the `App\Validators\UserValidator` class with specific rules for updating a user:
0 commit comments