0
votes

I'm busy migrating an app from Laravel 4.2 to 5.1, and am experiencing an issue with Form Requests. It's probably something simple that I'm missing - not really sure.

The method below is meant to attempt a sign in if SignInRequest successfully authorises the request. However, if the validation passes, SignInRequest is not passed to attemptSignIn, which throws the process off with this error:

Argument 2 passed to App\Http\Controllers\Auth\AuthController::attemptSignIn() must be an instance of App\Http\Requests\SignInRequest, none given

This is the method (controller is AuthController) in question, which tries to sign in using a username or an email address:

public function attemptSignIn($type = 'regular', SignInRequest $request)
{
    switch ($type) {
        case 'regular':
            $identifierFieldName = 'account_identifier';
            $field = filter_var($request->input($identifierFieldName), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
            $request->merge([$field => $request->input($identifierFieldName)]);
            $specifics = $request->only($field, 'passphrase');
            $specifics['activated'] = (int) true;
            if ($this->auth->attempt($specifics)) {
                return redirect()->intended($this->redirectPath);
            } else {
                return redirect($this->signInPath)
                    ->with('authError', "The credentials you've provided are incorrect.")
                    ->with('authErrorType', 'warning')
                    ->withInput($request->only($identifierFieldName))
                    ->withErrors();
            }
            break;

        case 'oota':

            break;

        default:
            return redirect($this->signInPath);
            break;
    }
}

The form request is straight-forward, specifying rules, messages and authorize (return true).

If the validation fails, however, SignInRequest is passed to the method, and the errors are shown accordingly.

Am I missing anything here?

1
did you inject SignInRequest in your controller? - mdamia
attemptSignIn is a controller method. Are you saying I need to also inject it into the constructor? - Mike Rockétt
The second arg SignInRequest passed to the method has to be injected or int at constructor - mdamia
So, I should assign $this->request to the form request in the constructor? if so, I would be unable to withErrors() in attemptSignIn, unless errors are passed to the view automatically... - Mike Rockétt
you don't need to pass the errors, they are passed by default. https://laracasts.com/series/whats-new-in-laravel-5/episodes/3 check the video, it s very helpful. - mdamia

1 Answers

1
votes
use \path\to\your\ClassRequest as MyRequest

or

protected $myrequest;

public function __construct(\path\to\your\ClassRequest $request){
   $this -> myrequest = $request; // use directly in your method or any method
}

Hope this help.

public function post_form(MyRequest $request) {
  your code here

}