Laravel框架表單驗(yàn)證操作實(shí)例分析
本文實(shí)例講述了Laravel框架表單驗(yàn)證操作。分享給大家供大家參考,具體如下:
public function create(Request $request){ if($request->isMethod('POST')){ //驗(yàn)證通過(guò)后繼續(xù)進(jìn)行 //方法1 控制器驗(yàn)證 $this->validate($request,[ 'Student.name' => 'required|min:2|max:20', 'Student.age' => 'required|integer', 'Student.sex' => 'required|integer', ],[ 'required'=>':attribute 為必填項(xiàng)', 'min' => ':attribute 長(zhǎng)度不符合要求', 'integer' => ':attribute 必須是一個(gè)整形', ],[ 'Student.name' => '姓名', 'Student.age' => '年齡', 'Student.sex' => '性別', ]); //方法2 Validator類驗(yàn)證 $validator = \Validator::make($request->input(),[ 'Student.name' => 'required|min:2|max:20', 'Student.age' => 'required|integer', 'Student.sex' => 'required|integer', ],[ 'required'=>':attribute 為必填項(xiàng)', 'min' => ':attribute 長(zhǎng)度不符合要求', 'integer' => ':attribute 必須是一個(gè)整形', ],[ 'Student.name' => '姓名', 'Student.age' => '年齡', 'Student.sex' => '性別', ]); if($validator->fails()){ return redirect()->back()->withErrors($validator)->withInput(); } //如果驗(yàn)證通過(guò),則繼續(xù)執(zhí)行下面的代碼 $data = $request->input('Student'); if(Student::create($data)){ return redirect('student/index')->with('success','添加成功'); }else{ return redirect()->back(); } } return view('student.create'); }
注意Laravel的create方法,需要在model中
設(shè)置允許批量賦值:
protected $fillable = ['name','age'];
web中間件有個(gè)作用是防止xss攻擊,即csrf,需要在頁(yè)面的表單中增加{{ csrf_field() }},
會(huì)生成一個(gè)隱藏的input表單,帶個(gè)token字段。
錯(cuò)誤信息的顯示:
控制器中的with方法,可以把信息放入session中
return redirect('Student/index')->with('success','添加成功');
頁(yè)面中的顯示:
@if(Session::has('success')) <div> {{ Session::get('success') }} </div> @endif @if(count($errors)) <div class="alert alert-danger"> <ul> @foreach($errors->all() as $val) <li>{{$val}}</li> @endforeach </ul> </div> @endif
{{$errors->first()}}
可以顯示第一條錯(cuò)誤信息
數(shù)據(jù)保持:
return redirect()->back()->withErrors($validator)->withInput();
這段代碼中的withInput可以把表單提交的信息帶回去,
頁(yè)面中使用old方法:
姓名 :<input type="text" name="Student[name]" value="{{ old('Student')['name'] }}" /> 性別 : @foreach($student->user_sex() as $ind=>$val) <input type="radio" name="Student[sex]" value="{{$ind}}" {{ old('Student')['sex']==$ind?'checked':'' }} />{{$val}} @endforeach
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel框架FormRequest中重寫(xiě)錯(cuò)誤處理的方法
- Laravel中使用FormRequest進(jìn)行表單驗(yàn)證方法及問(wèn)題匯總
- Laravel框架表單驗(yàn)證詳解
- Laravel 5框架學(xué)習(xí)之表單驗(yàn)證
- Laravel 4 初級(jí)教程之Pages、表單驗(yàn)證
- laravel5.2表單驗(yàn)證,并顯示錯(cuò)誤信息的實(shí)例
- Laravel框架表單驗(yàn)證格式化輸出的方法
- Laravel5.1 框架表單驗(yàn)證操作實(shí)例詳解
- laravel框架使用FormRequest進(jìn)行表單驗(yàn)證,驗(yàn)證異常返回JSON操作示例
相關(guān)文章
PHP超全局?jǐn)?shù)組(Superglobals)介紹
這篇文章主要介紹了PHP超全局?jǐn)?shù)組(Superglobals)介紹,本文講解了概述、變量的作用域、超全局?jǐn)?shù)組及注意事項(xiàng)等內(nèi)容,需要的朋友可以參考下2015-07-07php從數(shù)據(jù)庫(kù)讀取數(shù)據(jù),并以json格式返回?cái)?shù)據(jù)的方法
今天小編就為大家分享一篇php從數(shù)據(jù)庫(kù)讀取數(shù)據(jù),并以json格式返回?cái)?shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法詳解
本篇文章是對(duì)AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05TP5框架實(shí)現(xiàn)一次選擇多張圖片并預(yù)覽的方法示例
這篇文章主要介紹了TP5框架實(shí)現(xiàn)一次選擇多張圖片并預(yù)覽的方法,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP5基于ajax數(shù)據(jù)提交上傳多張圖片與本地預(yù)覽相關(guān)操作技巧,需要的朋友可以參考下2020-04-04使用ThinkPHP的自動(dòng)完成實(shí)現(xiàn)無(wú)限級(jí)分類實(shí)例詳解
這篇文章主要介紹了使用ThinkPHP的自動(dòng)完成實(shí)現(xiàn)無(wú)限級(jí)分類功能,本文通過(guò)圖文并茂的方式給大家展示,介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09