Laravel表单验证似乎不起作用

时间:2016-10-29 18:17:15

标签: php forms laravel laravel-5 laravel-validation

我在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似乎很好,带有令牌和有效的URL

<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 个答案:

答案 0 :(得分:1)

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

您将其从无效的Request课程中扩展出来。您从\Illuminate\Http\Request类扩展它,但是您应该从\Illuminate\Foundation\Http\FormRequest(或\App\Http\Requests\Request类)扩展它