laravel表单验证

laravel表单验证的两种方法

方法1:直接在控制器中写方法

直接在需要用到表单验证的控制器里面编写代码 如下所示在indexController控制器中 用show方法接收到表单传过来的数据,进行验证

//indexController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;  //引用的时候一定要注意看路径 其他的Validator都不行
class indexController extends Controller
{
    public function show(Request $res){
        //定义验证规则
        $rules = [
            'username' => 'required | max:20',
            'password' => 'required'
        ];
        //定义报错信息
        $messages = [
            'username.required' => '用户名不能为空',
            'username.max' => '用户名长度最大不能超过20',
            'password.required' => '密码不能为空'
        ];
        //将$res里面传过来的数据进行判断
        $validator =  Validator::make($res->all(),$rules,$messages);
        
        //如果判断为真  就会跳到控制器error
        if ($validator->fails()){
            return redirect('error')->withErrors($validator);
        }
    }
}

方法1的路由:

//路由
Route::get('/', [\App\Http\Controllers\indexController::class,'index']);
Route::post('show',[\App\Http\Controllers\indexController::class,'show']);

Route::get('error',function(){
    return view('error');
});

方法1的error视图:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
@if($errors->any())
    <ul>
        @foreach($errors->all() as $error)
            <li>{{$error}}</li>
        @endforeach
    </ul>
@endif
</body>
</html>

编写完以上代码之后,在表单页面提交,如果不符合表单验证就会跳到error试图。否则正常执行。

方法2:创建请求文件(推荐)

php artisan make:request requestName
requestName 为自己定义的文件名

在新建的请求文件中编写以下代码

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ckform extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;   //这里一定要改成true默认是false 不然会报403错误
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'username' => 'required | max:20',
            'password' => 'required'
        ];
    }

    public function messages()
    {
        return [
            'username.required' => '姓名必填',
            'username.max' => '最大长度不能超过20',
            'password.required' => '密码必填',
        ];
    }
}

控制器中代码如下:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\ckform;
class indexController extends Controller
{
    public function index(){
        return view('index');
    }
    public function show(ckform $res){  //ckfrom就是我新建的请求文件
        return 'ok';  //如果没有错误即正常执行抛出ok
    }
}

如果表单提交的数据不符合请求规则

则会重定向到上一个页面,我这里上一个页面就是提交数据的页面

我们可以在提交数据的这个页面接收报错信息并打印出来,代码如下所示:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
@if($errors->any())
    <ul>
        @foreach($errors->all() as $error)
            <li>{{$error}}</li>
        @endforeach
    </ul>
@endif
    <form action="show" method="POST">
        @csrf
        <input type="text" name="username" value="chenXin">
        <input type="text" name="password" value="888888">
        <input type="submit" value="提交">
    </form>
</body>
</html>

两种方法更推荐第二种,兼容性更高,代码量更少逻辑,更加清晰~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值