如何在Laravel中的个人资料中更改电子邮件地址表格 [英] How to make change email address form in profile in Laravel
问题描述
我成功完成了密码更改,但是我遇到了更改电子邮件的麻烦,我认为这会比较容易,但是由于某些原因却无法考虑该怎么做.
I did password change successfully but i had troubles for email change which i think it would be easier but for some reason couldn't think how to do it.
旧电子邮件地址:
新电子邮件:
确认新电子邮件:
这是我想要的形式,但无法解决Controllers功能.
This is the form i wanna make it but cant solve the Controllers function.
这就是我试图解决的方法
This is how i tried to solve it
public function changeemailPost(Request $request)
{
if (!(Hash::check($request->get('email'), Auth::user()->email))) {
return redirect()->back()->with("error","Your current e-mail does not matches with the e-mail you provided. Please try again.");
}
if(strcmp($request->get('email'), $request->get('new-email')) == 0){
return redirect()->back()->with("error","New e-mail cannot be same as your current e-mail. Please choose a different e-mail.");
}
$validate = $request->validate([
'email' => 'required',
'new-email' => 'required|string|min:6|confirmed',
]);
$user = Auth::user();
$user->email = $request['new-email'];
$user->save();
Auth::logout();
return view('auth.login');
}
这是html
<form method="POST" action="{{ route('change-emailPost') }}">
@csrf
<label>Enter your old e-mail:</label>
<fieldset class="youplay-input">
<input id="current-email" type="email" class="form-control" placeholder="Current E-Mail" name="email" required>
</fieldset>
<label>Enter your new e-mail:</label>
<fieldset class="youplay-input">
<input id="new-email" type="email" placeholder="New E-Mail" class="form-control" name="new-email" required>
</fieldset>
<fieldset class="youplay-input">
<input id="new-email-confirm" type="email" class="form-control" placeholder="Confirm New E-Mail" name="new-email-confirmation" required>
</fieldset>
<button type="submit" class="btn btn-default db">
Change E-Mail
</button>
</form>
但我只会收到此错误
您当前的电子邮件与您提供的电子邮件不匹配.请重试
Your current e-mail does not matches with the e-mail you provided. Please try again
由于以下回答,该问题已得到解决,但是您应该更改的一件事是哈希检查电子邮件中的错误,而不是:
if (!(Hash::check($request->get('email'), Auth::user()->email))) {
return redirect()->back()->with("error","Your current e-mail does not matches with the e-mail you provided. Please try again.");
}
if(strcmp($request->get('email'), $request->get('new-email')) == 0){
return redirect()->back()->with("error","New e-mail cannot be same as your current e-mail. Please choose a different e-mail.");
}
使用此功能:
if(Auth::user()->email != $request['email']){
return redirect()->back()->with("error","Your email doesnt not match with what you provided");
}
if(Auth::user()->email === $request['new-email']){
return redirect()->back()->with("error", "Your new email cannot be the same as the old email");
}
推荐答案
您需要更改您的 changeemailPost 功能.试试这个-
You need change your changeemailPost function. Try this -
public function changeemailPost(Request $request)
{
if (!(Hash::check($request->get('email'), Auth::user()->email))) {
return redirect()->back()->with("error","Your current e-mail does not matches with the e-mail you provided. Please try again.");
}
if(strcmp($request->get('email'), $request->get('new-email')) == 0){
return redirect()->back()->with("error","New e-mail cannot be same as your current e-mail. Please choose a different e-mail.");
}
$validate = $request->validate([
'new-email' => 'required|string|min:6|confirmed',
'new-email_confirmation' => 'required|string|min:6',
]);
$user = Auth::user();
$user->email = $request['new-email'];
$user->save();
Auth::logout();
return view('auth.login');
}
还有如下所示的HTML-
And your HTML like below -
<form method="POST" action="{{ route('change-emailPost') }}">
@csrf
<label>Enter your old e-mail:</label>
<fieldset class="youplay-input">
<input id="current-email" type="email" class="form-control" placeholder="Current E-Mail" name="email" required>
</fieldset>
<label>Enter your new e-mail:</label>
<fieldset class="youplay-input">
<input id="new-email" type="email" placeholder="New E-Mail" class="form-control" name="new-email" required>
</fieldset>
<fieldset class="youplay-input">
<input id="new-email-confirm" type="email" class="form-control" placeholder="Confirm New E-Mail" name="new-email_confirmation" required>
</fieldset>
<button type="submit" class="btn btn-default db">
Change E-Mail
</button>
</form>
如果要比较具有confirmed
验证规则的值的两个输入字段a & b
,则应将b
字段重命名为b_confirmation
.
If you want to compare two input field a & b
with value with confirmed
validation rules b
field should be renamed as b_confirmation
.
参考- https://laravel.com/docs/7.x /validation#rule-confirmed
这篇关于如何在Laravel中的个人资料中更改电子邮件地址表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!