Custom redirect after registration validation failure - Call to undefined method Illuminate\Http\RedirectResponse::validate()

Published 5 days ago by Calid

I am using modal for registration and login, I want to redirect user to /registration page if validation has an error. I am getting an error

Call to undefined method Illuminate\Http\RedirectResponse::validate()

Here is my RegisterController.php

<?php
namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Http\Request;

use Validator;
use Auth;

class RegisterController extends Controller
{

    use RegistersUsers;
    
    protected $redirectTo = '/home';

    
    public function __construct()
    {
        $this->middleware('guest');
    }

    
    protected function validator(array $data)
    {
        $validator = Validator::make($data, [
            'username' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,'.Auth::id(),
            'password' => 'required|string|min:6',
            'country_id' => 'required|integer|exists:countries,id',
            'mobile' => 'required|numeric|min:6|max:15',
        ]);
        
        
        //return $validator;
        
        if ($validator->fails()) {
            return redirect('/register')
            ->withErrors($validator)
            ->withInput();
        }

    }

    
    
    protected function create(array $data)
    {
            $user = new User;
            $user->username = $data['username'];
            $user->email = $data['email'];
            $user->password = Hash::make($data['password']);
            $user->mobile = $data['email'];
            $user->country_id = $data['country_id'];
            $user->ip = request()->ip();

            if($user->save()) {
            return $user;
            }
    }
}

Thanks

Best Answer (As Selected By Calid)
Snapey

This is the function in the RegistersUsers trait

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();
        event(new Registered($user = $this->create($request->all())));
        $this->guard()->login($user);
        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

This code $this->validator($request->all())->validate(); is the line that expects a validator instance

It looks like you should put the validation back as it was and then add your own version of the register function

Snapey
Snapey
5 days ago (993,535 XP)

looks like you are returning a response object from the validator function when the caller is expecting a validator instance

Snapey
Snapey
5 days ago (993,535 XP)

This is the function in the RegistersUsers trait

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();
        event(new Registered($user = $this->create($request->all())));
        $this->guard()->login($user);
        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

This code $this->validator($request->all())->validate(); is the line that expects a validator instance

It looks like you should put the validation back as it was and then add your own version of the register function

InspiredPrynce

Reverse the registration trait back to the way it was... @Snapey Is right

InspiredPrynce

Change your function to this

protected function validator(array $data)
    {
    return Validator::make($data, [
           'username' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,'.Auth::id(),
            'password' => 'required|string|min:6',
            'country_id' => 'required|integer|exists:countries,id',
            'mobile' => 'required|numeric|min:6|max:15',
        ]);
    }

//If you want to retain your form data if validator fails, do this

<input type="text" name="email" value="{{ old('email') }}" required>

Hope this helps! Best regards

Calid
Calid
4 days ago (11,210 XP)

Thanks you @Snapey , added this to RegisterController.php

    /**
     *
     * Override Trait RegistersUsers : vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php
     *
    */
    public function register(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            return redirect('/register')
            ->withErrors($validator)
            ->withInput();
        }

        event(new Registered($user = $this->create($request->all())));
        $this->guard()->login($user);
        return redirect($this->redirectPath());
    }

Please sign in or create an account to participate in this conversation.