How to use Custom Request in Controller

Asked

Viewed 63 times

0

Follows my AuthRequest.php:

<?php

namespace App\Http\Requests;

use Illuminate\Http\Request;
use App\Rules\CpfValidacao;

class AuthRequest extends Request
{

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email'    => 'required|min:1|max:255',
            'cpf'      => ['required', 'min:1', 'max:11', new CpfValidacao],
            'name'     => 'required|min:1|max:255',
            'password' => 'required|min:8|max:255'
            //'profile_picture_path' => 'required|min:1|max:255'
        ];
    }

    /*** Novas mensagens ***/
    public function messages()
    {
        return [
                'email.required'    => 'Campo email requerido!',
                'email.min'        => 'Campo email deve ser de no mínimo 1 caracter!',
                'email.max'        => 'Campo email deve ser de no máximo 255 caracteres!',
                'cpf.required'      => 'Campo cpf requerido!',
                'email.min'        => 'Campo cpf deve ser de no mínimo 1 dígito!',
                'email.max'        => 'Campo cpf deve ser de no máximo 11 dígitos!',
                'name.required'     => 'Campo name requerido!',
                'name.min'         => 'Campo name deve ser de no mínimo 1 caracter!',
                'name.max'         => 'Campo name deve ser de no máximo 255 caracteres!',
                'password.required' => 'Campo password requerido!',
                'password.min'     => 'Campo password deve ser de no mínimo 8 caracteres!',
                'password.max'     => 'Campo password deve ser de no máximo 255 caracteres!',
        ];
    }



}

My doubt is here:

/*** Registra usuário ***/
    public function register(AuthRequest $request)
    {

        /*** validação de parâmetros ***/
        $validator = Validator::make($request->all(), [
            'email'    => 'required|min:1|max:255',
            'cpf'      => ['required', 'min:1', 'max:11', new CpfValidacao],
            'name'     => 'required|min:1|max:255',
            'password' => 'required|min:8|max:255'
            //'profile_picture_path' => 'required|min:1|max:255'
        ]);


        if($validator->fails()){
            $errors = $validator->erros();
            return response()->json(['error' => true, $errors], 422);
        }
        /*** fim ***/

How I do the new implementation?

laravel 6.2

  • Question doesn’t say what you want?

  • I don’t understand very well, you want to use Authregisterrequest.php to validate user creation?

1 answer

2


The question is not very clear, otherwise what you’re looking for edits the question, okay? From what I understand you want to validate the user by following the rules of AuthRegisterRequest.php. So include the request file

use App\Http\Requests\AuthRegisterRequest;

Put the function parameter you want to validate as AuthRegisterRequest:

public function register(AuthRegisterRequest $request){}

And call inside the function register the validation method:

public function register(AuthRegisterRequest $request)
   {
        $validated = $request->validated();
   }

However, if it’s not necessary to work with the validated data itself within the controller, that is, if you just want to return the error if there is a validation problem, you can abstract this previous code in the Controller and create a method within the AuthRegisterRequest.php to do it always. That way:

public function validation(Request $request, AuthRegisterRequest $Object)
{
    $validator = Validator::make($request->all(), $Object->rules(), [], $Object->names());

    if ($validator->fails())
        return $validator->errors();
    return true;
}

With this, none of your validation rules will need to be written in the controller.

  • Yeah, my idea is to take that part of the validator controller, using an external file

  • apparently after the change I made, $validator = Validator::make($request->all(), $Object->rules(), $Object->messages());, The question of messages does not yet work, still appear in English

  • you checked the Authregisterrequest.php import in the controller?

  • A small change before: I renamed AuthRegisterRequest for AuthRequest. I imported it as follows: use App\Http\Requests\AuthRequest;. laravel 6.2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.