Validation classes Laravel

0

Good morning !!

I have been using some validation classes for a long time (CPF, e-mail, CNPJ) at the moment I am starting to migrate to laravel and the doubt has arisen. Where do I put these validation classes?

What good does laravel practice recommend? I put the classes in a certain directory and I call the? (if so which directory) Or should I put these methods in a controller or in a middleware?

What good does laravel practice recommend?

Thank you very much

    
asked by anonymous 13.12.2018 / 11:25

2 answers

1

You can create a request class that will be responsible for this, how? Follow the Laravel documentation link: link

Example:

Your controller will have a method of creating a new user.

public function createUser(UserRequest $request){
      // logica para criar o usuário
}

* Note, instead of using the default Request $request , you create a class, which you modify as you wish, in it you pass the required error messages and fields.

How to create a class Request ? use the artisan command: php artisan make:request NomeDaRequest , by default it will be created in the request/ folder just look in your IDE you will find.

Within this new Request class, you have 2 methods that you will use to send field error messages and the fields called required, they are rules() and messages() . The rules() it checks the required fields and messages() displays the error messages.

public function messages(){
    return [
          'title.required' => 'A title is required',
          'body.required'  => 'A message is required',
    ];
}

public function rules(){
       return [
              'title' => 'required|unique:posts|max:255',
              'body' => 'required',
       ];
}
    
13.12.2018 / 13:17
0

Friend ... you have to create a custom validation.

Take a look here:

link

It's easy and organized ... I usually put a folder in App \ Validations and create an AppValidator class ... within that class it puts my custom validations ...

Any questions ...

    
13.12.2018 / 16:16