针对上一节的表单添加验证功能
File:plugins\raiseinfo\contact\components\ContactForm.php use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Redirect; ... public function onSend() { $validator = Validator::make( [ 'name' => Input::get('name'), 'email' => Input::get('email') ], [ 'name' => 'required|min:5', 'email' => 'required|email' ] ); if($validator->fails()){ return Redirect::back()->withErrors($validator); }else{ $vars = [ 'name' => Input::get('name'), 'email' => Input::get('email'), 'content' => Input::get('content') ]; // views/mail/message/htm Mail::send('raiseinfo.contact::mail.message', $vars, function($message) { $message->to('[email protected]', 'Admin Person'); $message->subject('从联系人表单提交的邮件'); }); } }
显示错误信息
File:plugins\raiseinfo\contact\components\contactform\default.htm <form data-request="onSend"> <label>姓名:</label> <input type="text" name="name" class="form-control"> {{ errors.first('name') }} <label>邮件地址:</label> <input type="email" name="email" class="form-control"> {{ errors.first('email') }} <label>信息:</label> <textarea name="content" class="form-control"></textarea> <button type="submit" class="btn btn-success form-control">发送</button> <ul> {% for error in errors.all() %} <li>{{ error }}</li> {% endfor %} </ul> </form>