为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Laravel 的个人资料中更改电子邮件地址表单

如何在 Laravel 的个人资料中更改电子邮件地址表单

PHP
ibeautiful 2023-04-21 15:03:36
我成功更改了密码,但我在更改电子邮件时遇到了麻烦,我认为这会更容易,但出于某种原因我想不出该怎么做。旧邮件:新邮箱:确认新邮箱:这是我想制作的形式,但无法解决 Controllers 功能。这就是我试图解决它的方式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>但我只得到这个错误您当前的电子邮件与您提供的电子邮件不匹配。请再试一次
查看完整描述

1 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

您需要更改您的changemailPost功能。尝试这个 -


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 如下所示 -


<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>

如果要比较两个输入字段a & b的值和confirmed验证规则b字段应重命名为b_confirmation.


查看完整回答
反对 回复 2023-04-21
  • 1 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信