Laravel5.1 框架表单验证操作实例详解
吾爱主题
阅读:238
2021-09-26 13:41:00
评论:0
本文实例讲述了Laravel5.1 框架表单验证操作。分享给大家供大家参考,具体如下:
当我们提交表单时 通常会对提交过来的数据进行一些验证、Laravel在Controller类中使用了一个traint:ValidatesRequest。方便我们在控制器中使用验证器。
下面我们就来看一个验证表单的例子。
1 准备
1.1 创建路由
?1 | Route::resource( '/post' , 'PostController' ); |
1.2 创建控制器
?1 | php artisan make:controller PostController |
1.3 创建视图
在 /views 中创建 /post/create.blade.php 文件,编写如下:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <!DOCTYPE html> <html> <head> <link rel= "stylesheet" href= "//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" 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" > 创建文章 </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" >标题</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" >内容</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视图
?1 2 3 4 | public function create() { return view( 'post.create' ); } |
2 开始验证
2.1 validate
我们在store方法中验证表单提交过来的数据,语法是这样的:
validate() 参数:
- request:传入请求就好。
- rule:规则数组,把我们的验证逻辑写在这里面。
1 2 3 4 5 6 7 8 | public function store(Request $request ) { $this ->validate( $request , [ 'title' => 'required|min:3' , 'content' => 'required|min:10' , ]); echo '验证通过' ; } |
↑ 上面的例子如果验证通过 则显示"验证通过" 如果验证没有通过的话Laravel会自动跳转到表单提交页面 并把错误信息闪存到Session中,我们可以修改create.balde.php文件 添加显示错误代码
2.2 显示错误信息
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <!DOCTYPE html> <html> <head> <link rel= "stylesheet" href= "//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" 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" > 创建文章 </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" >标题</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" >内容</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 手动创建Validator
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 '验证通过' ; } |
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
原文链接:https://www.cnblogs.com/sun-kang/p/7614766.html
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。