Laravel5.1 框架表單驗證操作實例詳解
本文實例講述了Laravel5.1 框架表單驗證操作。分享給大家供大家參考,具體如下:
當(dāng)我們提交表單時 通常會對提交過來的數(shù)據(jù)進(jìn)行一些驗證、Laravel在Controller類中使用了一個traint:ValidatesRequest。方便我們在控制器中使用驗證器。
下面我們就來看一個驗證表單的例子。
1 準(zhǔn)備
1.1 創(chuàng)建路由
Route::resource('/post', 'PostController');
1.2 創(chuàng)建控制器
php artisan make:controller PostController
1.3 創(chuàng)建視圖
在 /views 中創(chuàng)建 /post/create.blade.php 文件,編寫如下:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading"> 創(chuàng)建文章 </div> <div class="panel-body"> <form action="{{ url("/post") }}" method="POST" class="form-horizontal"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group"> <label class="col-md-4 control-label">標(biāo)題</label> <div class="col-md-6"> <input type="text" class="form-control" name="title"> </div> </div> <div class="form-group"> <label class="col-md-4 control-label">內(nèi)容</label> <div class="col-md-6"> <textarea rows="10" class="form-control" name="content"></textarea> </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <button class="btn btn-primary" type="submit">Submit</button> </div> </div> </form> </div> </div> </div> </div> </div> </body> </html>
1.4 在PostController中返回create視圖
public function create() { return view('post.create'); }
2 開始驗證
2.1 validate
我們在store方法中驗證表單提交過來的數(shù)據(jù),語法是這樣的:
validate() 參數(shù):
- request:傳入請求就好。
- rule:規(guī)則數(shù)組,把我們的驗證邏輯寫在這里面。
public function store(Request $request) { $this->validate($request, [ 'title' => 'required|min:3', 'content' => 'required|min:10', ]); echo '驗證通過'; }
↑ 上面的例子如果驗證通過 則顯示"驗證通過" 如果驗證沒有通過的話Laravel會自動跳轉(zhuǎn)到表單提交頁面 并把錯誤信息閃存到Session中,我們可以修改create.balde.php文件 添加顯示錯誤代碼
2.2 顯示錯誤信息
<!DOCTYPE html> <html> <head> <link rel="stylesheet" rel="external nofollow" rel="external nofollow" > </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading"> 創(chuàng)建文章 </div> <div class="panel-body"> @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ url("/post") }}" method="POST" class="form-horizontal"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group"> <label class="col-md-4 control-label">標(biāo)題</label> <div class="col-md-6"> <input type="text" class="form-control" name="title"> </div> </div> <div class="form-group"> <label class="col-md-4 control-label">內(nèi)容</label> <div class="col-md-6"> <textarea rows="10" class="form-control" name="content"></textarea> </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <button class="btn btn-primary" type="submit">Submit</button> </div> </div> </form> </div> </div> </div> </div> </div> </body> </html>
3 手動創(chuàng)建Validator
public function store(Request $request) { // $this->validate($request, [ // 'title' => 'required|min:3', // 'content' => 'required|min:10', // ]); $validator = Validator::make($request->all(), [ 'title' => 'required|min:3', 'content' => 'required|min:10', ]); if ($validator->fails()) { return redirect('post/create') ->withErrors($validator) ->withInput(); } echo '驗證通過'; }
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
上一篇:header函數(shù)設(shè)置響應(yīng)頭解決php跨域問題實例詳解
欄 目:PHP編程
本文標(biāo)題:Laravel5.1 框架表單驗證操作實例詳解
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/10978.html
您可能感興趣的文章
- 01-11thinkphp框架類庫擴(kuò)展操作示例
- 01-11關(guān)于Yii2框架跑腳本時內(nèi)存泄漏問題的分析與解決
- 01-11php 實現(xiàn)簡單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel框架Blade模板簡介及模板繼承用法分析
- 01-11Laravel框架基礎(chǔ)語法與知識點整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- 01-11laravel5.1框架下的批量賦值實現(xiàn)方法分析
- 01-11laravel5.5框架的上傳圖片功能實例分析【僅傳到服務(wù)器端】


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)