在Laravel的同一页面上有2个表单? [英] Having 2 forms on the same page in Laravel?

查看:58
本文介绍了在Laravel的同一页面上有2个表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用我的PHP代码解决一页上有两种形式"的问题,但事实证明它比我预期的要怪癖得多,并且行为方式不符合我的预期.

I'm trying to tackle a "two forms on one page" issue with my PHP code, but its turning out to be more tirkcy than I expected, and just isn't behaving in the correct way as I thought it would.

对于第一种形式(登录),我正在使用此if语句来确定是否为登录信息.

For the first form (Login) I'm using this if statement to determine if the message if for the Login.

@if(Session::has('message') && Session::get('last_message_for') == 'login')
<div class="notification is-{{ Session::get('color') }}">
    <i class="fa fa-times"></i> &nbsp;&nbsp;{{ Session::get('message') }}
</div>
@elseif($errors->first() && Session::get('last_message_for') == 'login')
<div class="notification is-warning">
    <i class="fa fa-times"></i> &nbsp;&nbsp;{{ $errors->first() }}
</div>
@endif

我的第二个表单具有相同的代码,但是它只是检查 last_message_for 的登录"值是否不同.

I've got the same code for my second form, but it just checks the last_message_for for a different value to 'login'.

@if(Session::has('message') && Session::get('last_message_for') == 'modal')
<div class="modal is-active" id="modal-forgotPassword">
@else 
<div class="modal" id="modal-forgotPassword">
@endif
    <div class="modal-background"></div>
    <div class="modal-card">
        <header class="modal-card-head">
            <p class="modal-card-title" id="open-modal">Forgot Password?</p> <button class="delete"></button>
        </header>
        <form action="{{ route('frontend.guest.password.forgot') }}" method="post">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <section class="modal-card-body">
                <div class="content">
                    @if(Session::has('message') && Session::get('last_message_for') == 'modal')
                    <div class="notification is-{{ Session::get('color') }}">
                        <i class="fa fa-times"></i> &nbsp;&nbsp;{{ Session::get('message') }}
                    </div>
                    @endif
                    <div class="field">
                        <p class="control has-icons-left">
                            <input class="input" name="email" placeholder="Enter an email..." type="email">
                            <span class="icon is-small is-left"><i class="fa fa-envelope"></i></span>
                        </p>
                    </div>
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                </div>
            </section>
            <footer class="modal-card-foot">
                <button class="button is-success" type="submit"><i class="fa fa-sign-in"></i> &nbsp;&nbsp;Send email</button>
            </footer>
        </form>
    </div>
</div>

现在解决这个问题,登录"部分可以很好地工作,并且在有错误提示时显示错误消息,但是第二部分在出现错误时却没有显示任何错误.

Now down to the issue, the Login part works perfectly fine and shows the error messages when there are any for it, but the second one isn't showing any errors when I have some.

我正在使用它来设置last_message_for

I'm using this to set the last_message_for

Session::put('last_message_for', 'login');

这是第二种形式的代码:

Here is the code for my second form:

public function onForgotPassword(Request $request) {
    $validator = Validator::make($request->all(), [
        'email' => 'required|email|exists:users,mail',
    ]);

    Session::put('last_message_for', 'modal');

    if ( $validator->fails()) {
        return redirect()->route('frontend.guest.login')->withErrors($validator->messages());;
    }
    else {
        Mail::to($request->input('email'))->send(new ForgotPasswordEmail());
        return redirect()->route('frontend.guest.login')->withMessage('Email Sent')->withColor('warning');
    }
}

推荐答案

您不处理验证错误,仅返回消息,应该这样做.

You're not handling validation errors, only the messages returned, this should do it.

@if(Session::has('message') && Session::get('last_message_for') == 'modal')
<div class="notification is-{{ Session::get('color') }}">
    <i class="fa fa-times"></i> &nbsp;&nbsp;{{ Session::get('message') }}
</div>
@elseif($errors->first() && Session::get('last_message_for') == 'login')
<div class="notification is-warning">
    <i class="fa fa-times"></i> &nbsp;&nbsp;{{ $errors->first() }}
</div>
@endif

这篇关于在Laravel的同一页面上有2个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆