figalo66
figalo66

Reputation: 195

How to add errors after FormRequests validation?

How to add errors after FormRequests validation?

password_repository->update() will return an error if the current passwords entered do not match.

password_repository->update() calls an external API.

I want to add an error in the controller depending on the return value of the repository.

In PasswordRequest, validation after calling the external API cannot be described, so I am in trouble.

For this reason I want to add an error in the controller after doing password_repository->update().

PasswordController.php

public function completeEdit(PasswordRequest $request)
{
    $input = $request->only(['password', 'new_password']);

    $data = $this->password_repository->update($input);

    //I want to add an error at this point!!!

    return view('pages.password.edit.complete');
    }

}

PasswordRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PasswordRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'password' => 'required',
            'new_password' => 'required|confirmed',
            'new_password_confirmation' => 'required',
        ];
    }
}

Upvotes: 1

Views: 58

Answers (1)

mrhn
mrhn

Reputation: 18976

Redirect with errors could help you.

return redirect()->back()->withErrors([
     'Password not correct',
]);

Or return to a specific route.

return redirect()->route('password.create')->withErrors([
     'Password not correct',
]);

Upvotes: 1

Related Questions