Calid
1098
5
Laravel

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

Posted 9 months 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

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

Reply to

Use Markdown with GitHub-flavored code blocks.