Having 2 forms on the same page in Laravel?

陌路散爱 提交于 2021-02-10 07:06:12

问题


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.

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

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.

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');
    }
}

回答1:


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



回答2:


I have two forms in the same page so you can easily assign it error to each form using flash session in your controller just use session like this line

use Session;

this code related to two search forms

//searchInHistory
 public function searchInHistory(){
    $date = Request()->all();
    $rules   = [
        'dateFrom'   =>'required',
        'dateTo'     =>'required',
    ];

    $validator = Validator($date,$rules);
    if ($validator->fails()){
        Session::flash('inError', 'inError');
        return redirect()
                    ->back()
                    ->withErrors($validator)
                    ->withInput();
    }else{

        $store = DB::table('stores')->select(
            'store_details.id',
            'store_details.status',
            'stores.id AS storeId',
            'stores.partNo',
            'stores.title',
            'store_details.serialNo',
            'store_details.created_at',
            'store_details.updated_at'
            )
            ->join('store_details', 'store_details.storeId', '=', 'stores.id')
            ->where('store_details.status','inside')
            ->whereBetween('store_details.created_at',[$date['dateFrom'],$date['dateTo']])
            ->get();
        return view('crm.store.in',compact('store'));
    }
}
//===============
//searchHistory
 public function searchOutHistory(){
    $date = Request()->all();
    $rules   = [
        'dateFrom'   =>'required',
        'dateTo'     =>'required',
    ];

    $validator = Validator($date,$rules);
    if ($validator->fails()){
        Session::flash('inError', 'inError');
        return redirect()
                    ->back()
                    ->withErrors($validator)
                    ->withInput();
    }else{
        $store = DB::table('stores')->select(
            'store_details.id',
            'store_details.status',
            'stores.id AS storeId',
            'stores.partNo',
            'stores.title',
            'store_details.serialNo',
            'store_details.created_at',
            'store_details.updated_at'
            )
            ->join('store_details', 'store_details.storeId', '=', 'stores.id')
            ->where('store_details.status','outside')
            ->whereBetween('store_details.updated_at',[$date['dateFrom'],$date['dateTo']])
            ->get();
        return view('crm.store.out',compact('store'));
    }
}
//===============

the following code above each error assigned to specific form using session this is blade code view code related to two form

  <!-- Main content -->
 <section class="content">
   <div class="row">
     <div class="col-xs-12">
      <div class="box">
          <div class="box-header">
            <h3 class="box-title">Store IN / OUT Control</h3>
          </div>
          <div class="row">
                <div class="col-md-6">
                  <div class="box-body">
                    @if(session('outError'))
                        @if ($errors->any())
                          <div class="alert alert-danger">
                              <center>
                                  @foreach ($errors->all() as $error)
                                      {{ $error }}<br>
                                  @endforeach
                              </center>
                          </div>
                        @endif
                    @endif
                    @if(session('out'))
                        @if(session('save'))
                        <div class="alert alert-success">
                            <center>
                               Products Came out of Successfully
                            </center>
                        </div>
                        @endif   
                    @endif

                       <!-- form start -->
                       <form role="form" method="post" action="{{url('admin/takeProductOutStore')}}" enctype="multipart/form-data">
                        {{csrf_field()}}
                        <div class="box-body">
                        <input type="hidden" name="id" value="{{$storeId}}">
                              <div class="form-group">
                                <label>Products in Store</label>
                                <select multiple class="form-control" name="products[]" required>
                                    @foreach($InDoorProducts as $row)
                                      <option value="{{$row->serialNo}}">{{$row->serialNo}}</option>
                                    @endforeach
                                </select>
                              </div>
                              <div class="form-group">
                                  <label>Date</label>
                                  <input type="date" class="form-control" name="date" required>
                              </div>
                              <div class="form-group">
                                  <textarea class="textarea" required placeholder=" Write Notes"style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" name="note" value="{{old('note')}}"></textarea>
                              </div>
                        </div><!-- /.box-body -->
                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">Out Store <b>&rarr;</b></button>
                          </div>
                       </form>
                      </div>
                </div>
                <div class="col-md-6">
                    <div class="box-body">
                        @if(session('inError'))
                        @if ($errors->any())
                          <div class="alert alert-danger">
                              <center>
                                  @foreach ($errors->all() as $error)
                                      {{ $error }}<br>
                                  @endforeach
                              </center>
                          </div>
                        @endif
                    @endif
                    @if(session('in'))
                        @if(session('save'))
                        <div class="alert alert-success">
                            <center>
                               Products Added to Store Again Successfully
                            </center>
                        </div>
                        @endif   
                    @endif

                    <!-- form start -->
                    <form role="form" method="post" action="{{url('admin/takeProductInStore')}}" enctype="multipart/form-data">
                      {{csrf_field()}}
                      <div class="box-body">
                          <input type="hidden" name="id" value="{{$storeId}}">

                            <div class="form-group">
                              <label>Products Out Store</label>
                              <select multiple class="form-control" name="products[]" required>
                                  @foreach($OutDoorProducts as $row)
                                    <option>{{$row->serialNo}}</option>
                                  @endforeach
                              </select>
                            </div>
                            <div class="form-group">
                                <label>Date</label>
                                <input type="date" class="form-control" name="date" required>
                            </div>
                            <div class="form-group">
                                <textarea class="textarea" required placeholder=" Write Notes"style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" name="note" value="{{old('note')}}"></textarea>
                            </div>
                          </div><!-- /.box-body -->

                      <div class="box-footer">
                          <button type="submit" class="btn btn-primary">Back To Store <b>&larr;</b></button>
                      </div>
                    </form>
                    </div>
                </div>
          </div>
          </section>


来源:https://stackoverflow.com/questions/46473235/having-2-forms-on-the-same-page-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!