Vaibhav Dhage
Vaibhav Dhage

Reputation: 902

Laravel Auth - Redirect User to different url from function validator() after validation fails

I am trying to redirect user to different page after Unique validation fails for mobile_no

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

class RegisterController extends Controller
    {
        use RegistersUsers;

        protected function validator(array $data){
            $validator = Validator::make($data, $rules, $messages);
            // If unique validation fails
            if($validator->fails()){ 
                $failed = $validator->failed();
                // if failed because unique mobile number validation rule
                if(count($failed) == 1){
                    foreach ($failed as $key => $value) {
                        if($key == 'mobile_no'){
                            if(count($value) == 1 && isset($value['Unique'])){
                                return redirect('/verify');
                            }
                        }
                    }
                }
            return redirect('/somepage');
            }
        return $validator;
    }
}

when I try to do this, it give error

BadMethodCallException in RedirectResponse.php line 218:
Method [validate] does not exist on Redirect.

How can I redirect to some other page? am I missing something.

Upvotes: 0

Views: 269

Answers (1)

sisve
sisve

Reputation: 19781

The validator method should return a Validator. The error you see is because you return a Redirect response, and some code presumes you return a Validator and calls $validator->validate() on it.

The actual call can be found in the RegistersUsers trait.

You need to throw an HttpResponseException instead. Your exception handler should catch these and render the response accordingly; in this case perform the redirect.

Upvotes: 1

Related Questions