2016-09-23 55 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,我将发送一个邮件,验证用户输入值,这就是为什么我使用手动方法。 –