Skip to content
Advertisement

Laravel Argument 1 passed to validator() must be an instance of IlluminateHttpRequest, array given

I had a register and login system without e mail verification on laravel. Today I wanted to add e-mail verification using this guide: https://mailtrap.io/blog/laravel-email-verification/ After everything finished I tried to register a user but I got error “Argument 1 passed to AppHttpControllersAuthRegisterController::validator() must be an instance of IlluminateHttpRequest, array given, called in /var/www/sinemori/vendor/laravel/ui/auth-backend/RegistersUsers.php on line 32” I googled error and most of the answers were about use IlluminateHttpRequest; which I already use.

On my web.php I am using

Auth::routes(['verify' => true]);
Auth::routes();

For /register route

Here is my RegisterController

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use AppModelsUser;
use IlluminateFoundationAuthRegistersUsers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;

class RegisterController extends Controller
{
use RegistersUsers;
public function __construct()
{
    $this->middleware('guest');
}

protected function validator(Request $request)
{
    $this->validate($request, ['name' => 'required|string|min:2|max:255',
        'surname' => 'required|string|min:2|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'username' => 'required|string|max:255|unique:users',
        'password' => 'required|string|min:8|confirmed',
        'password_confirmation' => 'required']);
}

public function store(Request $request){
    $this->validator($request);

    User::create([
        'name' => $request->name,
        'surname' => $request->surname,
        'email' => $request->email,
        'username' => $request->username,
        'password' => Hash::make($request->password),
        'profile_img' => "/images/users/default_profile_picture.png"
    ]);

    return redirect()->intended('/login');
}
}

Here is the register.blade.php

@if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif
<form method="post" action="{{ route('register') }}">
@csrf
<div class="form-group">
    <label>Name</label>
    <input type="text" name="name" class="form-control p_input">
</div>
<div class="form-group">
    <label>Surname</label>
    <input type="text" name="surname" class="form-control p_input">
</div>
<div class="form-group">
    <label>Email</label>
    <input type="email" name="email" class="form-control p_input">
</div>
<div class="form-group">
    <label>Username</label>
    <input type="text" name="username" class="form-control p_input">
</div>
<div class="form-group">
    <label>Password</label>
    <input type="password" name="password" class="form-control p_input">
</div>
<div class="form-group">
    <label>Password Again</label>
    <input type="password" name="password_confirmation" class="form-control p_input">
</div>
<div class="text-center">
    <button type="submit" class="btn btn-primary btn-block enter-btn">Register</button>
</div>
</form>

Advertisement

Answer

Look at the RegisterController.stub in /vendor/laravel/ui/stubs/Auth. The validator accepts an array, not a Request object, and that is what RegistersUsers passes in.

RegisterController.stub

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

And RegistersUsers

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    if ($response = $this->registered($request, $user)) {
        return $response;
    }

    return $request->wantsJson()
                ? new JsonResponse([], 201)
                : redirect($this->redirectPath());
}

So you want to match your function to the one in RegisterController.stub.

protected function validator(array $request)
{
    return Validator::make($request, ['name' => 'required|string|min:2|max:255',
        'surname' => 'required|string|min:2|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'username' => 'required|string|max:255|unique:users',
        'password' => 'required|string|min:8|confirmed',
        'password_confirmation' => 'required']);
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement