2017-07-26 56 views
0

我有一個使用Laravel 5.4,parsley.js和Bootstrap 3構建的聯繫表單,它工作正常,但它不會在成功交付時在表單頂部顯示成功消息的消息或在出現錯誤時顯示任何錯誤。請協助?在Laravel 5.4中未顯示警報消息

位指示

public function postIndex(Request $request){ 
     $this->validate($request, array(
       'name' => 'required|min:10', 
       'email' => 'required|email', 
       'message' => 'required|min:100' 
      )); 

     $name = $request->name; 

     $data = array(
      'name' => $request->name, 
      'email' => $request->email, 
      'bodymessage' => $request->message 
      ); 
     Mail::send('emails.contact', $data, function($message) use ($data) { 
      $message->from($data['email']); 
      $message->to('[email protected]'); 
     }); 

     Session::flash('success', 'Hello $name, Your Form was successfully sent'); 

     return redirect()->route('pages.index'); 
    } 

Index.blade.php

<div class="col-sm-4"> 

{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!} 

<div id="success"> 
<div class="col-sm-12"> 
@if(Session::has('success')) 

<div class="alert alert-success" role="alert"> 

<strong> Success: </strong> {{ Session::get('success') }} 

</div> 

@endif 

@if(count($errors) > 0) 

<div class="alert alert-danger" role="alert"> 

<strong> Errors: </strong> 
<ul> 
@foreach($errors->all() as $error) 
<li> {{ $error }} </li> 
@endforeach 
</ul> 

</div> 
@endif 
</div> 
</div> 

{{ Form::text('name', null, array('placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}} 

{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}} 

{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }} 

{{ Form::submit('Submit') }} 

{!! Form::close() !!} 
</div> 

路線

Route::get('/', '[email protected]') ->name('pages.index'); 
    Route::post('/', '[email protected]') ->name('index.post'); 

回答

0

您是重定向與表單發佈的路線相同。我假設呈現視圖的路由是GET路由並有另一個名稱,您應該重定向到該路由或呈現的路由index.blade.php

+0

,,,我已經糾正了路線和所使用的顯示形式,但仍然是錯誤信息不顯示,,,,這可能是我的Bootstrap代碼的錯誤 – Martin

+0

也許你可以發佈你的路線fil e和你的整個控制器 –

+0

,,,檢查出來我用新的路由和控制器更新了我的查詢,但問題仍然存在 – Martin

0

這就是爲什麼你有with()方法來鏈接route()方法。因此,而不是使用Session:flash(),你可以簡單地添加一個with()方法閃爍該消息會話的下一個請求:

return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent'); 

或者說,如果你的形式從index頁面本來,那麼你根本就沒有要記住路線的名稱,只需使用back() helper方法,即:

return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');