Search code examples
laravellaravel-validationlaravel-authentication

Laravel Call to a member function validate() on null


I'm getting this error on Laravel Auth register

Call to a member function validate() on null

The error only happens when the validation passes, when it fails it shows returns to the register view with the correct errors. I tried to dd after the if fails but it doesn't reach it:

protected function validator(array $data){
        $req = new Request($data);
        //dd($req,$data);
        $this->validate($req,
            [
                'name' => ['required', 'string', 'max:50','min:3'],
                'email' => ['required','email', 'unique:users'],
                'password' => ['required', 'min:8', 'confirmed'],
                'uni' => ['required'],
                'city' => ['required'],
            ],[
                'required' => 'هذا الحقل مطلوب',
                'email'=>'نمط البريد الالكتروني غير صحيح',
                'min'=>'يجب إدخال 8 حروف عالأقل',
                'email.unique' => 'هذا البريد الالكتروني مستخدم',
                'confirmed'=>'الرجاء التأكد من كلمة المرور',
                'max'=>'50 حرف هو أقصى حد يمكن إدخاله',
                'name.min'=>'الاسم قصير جدا',

            ]);
    }
    protected function create(array $data){
        if ($this->validator($data)->fails()) {
            return Redirect::back()->withErrors($this->validator($data))
                ->withInput();
        }
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'photo'=>'img/user.jfif',
            'university'=>$data['uni'],
            'city'=>$data['city'],
        ]);
    }

full code of RegisterController https://pastebin.com/B8XcNbBR
and RegistersUsers (where the stack trace shows the error) https://pastebin.com/BBTTStLL


Solution

  • Try this one

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:50','min:3'],
            'email' => ['required','email', 'unique:users'],
            'password' => ['required', 'min:8', 'confirmed'],
            'uni' => ['required'],
            'city' => ['required'],
        ],[
            'required' => 'هذا الحقل مطلوب',
            'email'=>'نمط البريد الالكتروني غير صحيح',
            'min'=>'يجب إدخال 8 حروف عالأقل',
            'email.unique' => 'هذا البريد الالكتروني مستخدم',
            'confirmed'=>'الرجاء التأكد من كلمة المرور',
            'max'=>'50 حرف هو أقصى حد يمكن إدخاله',
            'name.min'=>'الاسم قصير جدا',
    
        ]);
    }
    
    protected function create(array $data){ 
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'photo'=>'img/user.jfif',
            'university'=>$data['uni'],
            'city'=>$data['city'],
        ]);
    }