Reputation: 583
I have merged all login, create user, and forgot password on one and the same page, and for error handling the "email" input on the 3 forms should differ. I have sat the name on the email-input for the login form to: "email" as default. I have sat the name on create user to: "register-email" and altered the "validator" function in RegisterController so it looks like this:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'register-email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],
'register-password' => ['required', 'string', 'min:8', 'max:255', 'confirmed'],
]);
$validator->setAttributeNames([
'register-email' => 'email',
'register-password' => 'password',
]);
return $validator;
}
And it works like a charm!
Now i set the email-input name to "forgot-email" on the forgot-form but I don't know how to correct the ForgotPasswordController to handle this.
I have tried to overide the following functions, with the updated name:
protected function validateEmail(Request $request)
{
$request->validate(['forgot-email' => 'required|email']);
}
protected function credentials(Request $request)
{
return $request->only('forgot-email');
}
protected function sendResetLinkFailedResponse(Request $request, $response)
{
if ($request->wantsJson()) {
throw ValidationException::withMessages([
'forgot-email' => [trans($response)],
]);
}
return back()
->withInput($request->only('forgot-email'))
->withErrors(['forgot-email' => trans($response)]);
}
But i just get an error saying:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "forgot-email" does not exist LINE 1: select * from "users" where "forgot-email" = $1 limit 1 ^ (SQL: select * from "users" where "forgot-email" = [email protected] limit 1)
The problem is obviously, it thinks that the input-name is the same as the column name, and that's not the case here. So how do I tell the controller to use "forgot-email" as email?
Upvotes: 0
Views: 553
Reputation: 583
I found a solution. I just merge the forgot-email request to email request in the validateEmail function like this:
protected function validateEmail(Request $request)
{
$request->merge(['email' => $request->get('forgot-email')]);
$request->validate(['email' => 'required|email']);
}
protected function credentials(Request $request)
{
return $request->only('email');
}
protected function sendResetLinkFailedResponse(Request $request, $response)
{
if ($request->wantsJson()) {
throw ValidationException::withMessages([
'forgot-email' => [trans($response)],
]);
}
return back()
->withInput($request->only('email'))
->withErrors(['forgot-email' => trans($response)]);
}
UPDATE: This is only a part of a solution. The error is still send out as "email" when i do the @error in blade
UPDATE 2: Okay if the validation failed, the error was binded to "email", but for all other errors it was: "forgot-email" so I just changed the validate method to validate on "forgot-email" instead of email and now it seems to work properly. The validateEmail function now looks like this:
protected function validateEmail(Request $request)
{
$request->merge(['email' => $request->get('forgot-email')]);
$request->validate(['forgot-email' => 'required|email']);
}
Upvotes: 0