Laravel表单验证似乎不起作用

我正在Laravel制作我的第一张表格,表格的生成一切正常 . 只是商店功能似乎盲目地将用户返回到我的联系页面,与表单验证的结果无关 .

因此,如果发布的电子邮件地址不是电子邮件而是随机字符串,我仍然会返回/联系页面,并将感谢信息发送到视图 .

我的控制器看起来像这样:

namespace App\Http\Controllers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;

use App\Http\Requests\ContactFormRequest;

class ContactController extends Controller {

  public function create(){
      return view('contact');
  }

  public function store(ContactFormRequest  $request) {

      return \Redirect::route('contact')
        ->with('message', 'Thanks for contacting us!');

  }
}

像这样的表单处理程序:

namespace App\Http\Requests;
use Illuminate\Http\Request;

class ContactFormRequest extends Request {
  public function authorize() {
    return true;    // don't need to be registered to run form so true
  }
  public function rules() {
    return [
        'email' => 'required|email',
    ];
  }
}

这由以下路线控制

if (config('app.tan_site_page_contact')===true){
    Route::get('/contact', ['as' => 'contact', 'uses' => 'ContactController@create']);
    Route::post('/contact', ['as' => 'contact_store', 'uses' => 'ContactController@store']);
});

和这样的形式:

<ul>
    @foreach($errors->all() as $error)
      <li>{{ $error }}</li>
    @endforeach
   </ul>
   @if(Session::has('message'))
      <div class="alert alert-info">
        {{Session::get('message')}}
      </div>
   @endif


 {!! Form::open(array('route' => 'contact_store', 'id' => 'contactCallMeForm')) !!}         
 {!! Form::label('Your E-mail Address') !!}
 {!! Form::text('email', null,  array('required',  'class'=>'form-control',  'placeholder'=>'Your e-mail address')) !!}
 {!! Form::submit('Contact Us!',  array('class'=>'btn btn-primary')) !!} 
 {!! Form::close() !!}

表格html似乎很好,有一个令牌和有效的网址

<form method="POST" action="http://localhost/contact" accept-charset="UTF-8" id="contactCallMeForm" novalidate="">
    <input name="_token" type="hidden" value="VNHchLZhfsXadVZXCZWHGdAuJ4zgmO6cDJIGhR59">
    <label for="Your E-mail Address">Your E-mail Address</label>
    <input required="required" class="form-control" placeholder="Your e-mail address" name="email" type="text">
    <input class="btn btn-primary" type="submit" value="Contact Us!">
</form>

回答(1)

2 years ago

这里的问题是你的 ContactFormRequest 课程 .

您从无效 Request 类扩展它 . 你从 \Illuminate\Http\Request 类扩展它,但你应该从 \Illuminate\Foundation\Http\FormRequest (或 \App\Http\Requests\Request 类)扩展它