2016-09-23 54 views
0

我想在一個頁面的網站上使用laravel建立一個聯繫表單,我似乎無法獲得表單來驗證用戶輸入並顯示任何錯誤形式可能有。Laravel html表單不驗證表單輸入

email.blade.php:

<ul> 
    @foreach($errors->all() as $error) 
     <li>{{ $error }}</li> 
    @endforeach 
</ul> 
{!! Form::open(['route' => 'mail', 'method' => 'post', 'role' => 'form', 'id' => 'footer-form']) !!} 
<div class="form-group has-feedback"> 
    {!! Form::label('first_name', null, ['class' => 'sr-only']) !!} 
    {!! Form::text('first_name', null, ['class' => 'form-control', 'placeholder' => 'First Name']) !!} 
    <i class="fa fa-user form-control-feedback"></i> 
    @if($errors->has('first_name')) 
     {{ $errors->first('first_name') }} 
    @endif 
</div> 
<div class="form-group has-feedback"> 
    {!! Form::label('last_name', null, ['class' => 'sr-only']) !!} 
    {!! Form::text('last_name', null, ['class' => 'form-control', 'placeholder' => 'Last Name']) !!} 
    <i class="fa fa-user form-control-feedback"></i> 
    @if($errors->has('last_name')) 
     {{ $errors->first('last_name') }} 
    @endif 
</div> 
<div class="form-group has-feedback"> 
    {!! Form::label('email', null, ['class' => 'sr-only']) !!} 
    {!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email address']) !!} 
    <i class="fa fa-envelope form-control-feedback"></i> 
    @if($errors->has('email')) 
     {{ $errors->first('email') }} 
    @endif 
</div> 
<div class="form-group has-feedback"> 
    {!! Form::label('textarea', null, ['class' => 'sr-only']) !!} 
    {!! Form::textarea('textarea', null, ['class' => 'form-control', 'rows' => 8, 'placeholder' => 'Message']) !!} 
    <i class="fa fa-pencil form-control-feedback"></i> 
    @if($errors->has('textarea')) 
     {{ $errors->first('textarea') }} 
    @endif 
</div> 

{!! Form::submit('Send', ['class' => 'btn btn-default']) !!} 

{!! Form::close() !!} 

Route: web.php:

Route::get('/ensignhospital', [ 
    'as' => 'home', 
    'uses' => '[email protected]' 
]); 


Route::group(['before' => 'guest'], function() { 
    /* 
    * CSRF Protection 
    * 
    * */ 
    Route::group(['before' => 'csrf'], function() { 

     Route::post('/ensignhospital', [ 
      'as' => 'mail', 
      'uses' => '[email protected]' 
     ]); 
    }); 


}); 

controller to handle for request:

class HomeController extends Controller { 

    public function home(){ 
     return View('welcome'); 
    } 



    public function postSendMail(ContactFormRequest $request){ 

     if($request->fails()){ 

      return Redirect::route('') 
       ->withErrors() 
       ->withInput(); 

     }else{ 

      return View('passed'); 
     } 


    } 

} 

The form request validator class:

class ContactFormRequest extends FormRequest 
{ 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      // 
      'first_name' => 'required', 
      'last_name'  => 'required', 
      'email'   => 'required|email', 
      'message'  => 'required' 
     ]; 
    } 
} 

問題表單在我沒有輸入有效輸入時無法驗證。我需要表單來驗證用戶輸入和remain at the form position on the web page

please note:

我用.htaccess文件,以獲得這是對my computer顯示site rootlocalhost/mylaravelproject,而不是通常的localhost/mylaravelprojec/public一個通常會看到一個新安裝的laravel網站。

回答

0

從這裏的文件:https://laravel.com/docs/5.3/validation#form-request-validation

So, how are the validation rules evaluated? All you need to do is type-hint 
the request on your controller method. The incoming form request is validated 
before the controller method is called, meaning you do not need to clutter 
your controller with any validation logic. 

Laravel永遠不會真正留在表單上。它將改爲重定向,驗證然後重定向回錯誤。您的代碼中不需要檢查if($request->fails())

+0

你好@ollieread,我將發送一個郵件,驗證用戶輸入值,這就是爲什麼我使用手動方法。 –