数据库课程作业笔记 - 编写表单验证

表单验证

参考资料 Laravel 文档 - 表单验证

首先呢分析一下,表单来自 form 的提交,所以在我们的这个系统中,只有新增与修改,也就是 GET 与 PUT 方法需要对表单进行验证。
需要验证的有必填、输入字符、长度、上下限和存在这些需求。

这里说明一下表单验证 Request 类,这个类封装了 request 请求,在请求进来时会通过自带的 Validate 方法对 request 包含的字段做验证,成功就会被传入控制器执行控制器的方法,不成功会返回错误码(好像是402)和错误的 Session 并重定向面页,这时之前的面页包含的错误模板就会生效并输出错误。

创建表单

这里就展示购买记录的验证作为例子,其他根据需要做类似设置。

php artisan make:request PurchaseRequest

这里创建了一个 Request 文件,我们编辑一下

<?php
namespace App\Http\Requests;
use App\Models\Purchase;
use Illuminate\Foundation\Http\FormRequest;
class PurchaseRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
            case 'PUT':
                return [
                    Purchase::EID => 'integer|exists:employees,id',
                    Purchase::CID => 'integer|exists:customers,id',
                    Purchase::PID => 'integer|exists:products,id',
                    Purchase::TOTAL_PRICE => 'numeric|min:0|max:1000000',
                    Purchase::PTIME => 'date|nullable',
                    Purchase::QTY => 'integer|min:0|max:2147483647|nullable'
                ];
            case 'POST':
                return [
                    Purchase::EID => 'required|integer|exists:employees,id',
                    Purchase::CID => 'required|integer|exists:customers,id',
                    Purchase::PID => 'required|integer|exists:products,id',
                    Purchase::TOTAL_PRICE => 'required|numeric|min:0|max:1000000',
                    Purchase::PTIME => 'required|date|nullable',
                    Purchase::QTY => 'required|integer|min:0|max:2147483647|nullable'
                ];
            default:
                return [];
        }
    }
    public function messages()
    {
        return [
            'required' => ':attribute 需要填写',
            'numeric' => ':attribute 必须是数字',
            'max' =>  ':attribute 大于上限 :max',
            'exists' => ':attribute 不存在',
            'integer' => ':attribute 只能是整数',
            'min' => ':attribute 小于下限 :min'
        ];
    }
    public function attributes()
    {
        return [
            Purchase::EID => '员工',
            Purchase::CID => '顾客',
            Purchase::PID => '产品',
            Purchase::TOTAL_PRICE => '总价格',
            Purchase::PTIME => '购买时间',
            Purchase::QTY => '购买数量'
        ];
    }
}

解释说明

首先下面这段代码是做一个权限控制的,我们的逻辑比较简单不做判断,所以将原来的 false 改成 true

 public function authorize()
    {
        return true;
    }

接下来我们写 rule 方法,这里就是判断的设置了

 public function rules()
    {
        switch ($this->method()) {
            case 'PUT':
                return [
                    Purchase::EID => 'integer|exists:employees,id',
                    Purchase::CID => 'integer|exists:customers,id',
                    Purchase::PID => 'integer|exists:products,id',
                    Purchase::TOTAL_PRICE => 'numeric|min:0|max:1000000',
                    Purchase::PTIME => 'date|nullable',
                    Purchase::QTY => 'integer|min:0|max:2147483647|nullable'
                ];
            case 'POST':
                return [
                    Purchase::EID => 'required|integer|exists:employees,id',
                    Purchase::CID => 'required|integer|exists:customers,id',
                    Purchase::PID => 'required|integer|exists:products,id',
                    Purchase::TOTAL_PRICE => 'required|numeric|min:0|max:1000000',
                    Purchase::PTIME => 'required|date|nullable',
                    Purchase::QTY => 'required|integer|min:0|max:2147483647|nullable'
                ];
            default:
                return [];
        }
    }

可以看到 this->method() 将判断请求的方法,POST 是新增,PUT 是修改, 我们在 POST 的 Purchase::EID 中写了 required|integer|exists:employees,id
这里可以继续看一下文档下面的方法,required 表示字段必须存在,这个在PUT 里面就没有,因为 PUT 可以局部修改。
integer 表示整数,exists:employess,id 由于我们知道这是一个外键,所以必须存在于数据表employeeid 当中,不然就返回错误。

 public function messages()
    {
        return [
            'required' => ':attribute 需要填写',
            'numeric' => ':attribute 必须是数字',
            'max' =>  ':attribute 大于上限 :max',
            'exists' => ':attribute 不存在',
            'integer' => ':attribute 只能是整数',
            'min' => ':attribute 小于下限 :min'
        ];
    }

上面这一段写的是规则的别名,这里可以自定义我们需要的提示信息,:attribute 是占位符,这里会替换成对于的属性名。

public function attributes()
    {
        return [
            Purchase::EID => '员工',
            Purchase::CID => '顾客',
            Purchase::PID => '产品',
            Purchase::TOTAL_PRICE => '总价格',
            Purchase::PTIME => '购买时间',
            Purchase::QTY => '购买数量'
        ];
    }

上面则是属性的自定义名称。

接着我们完成其他的表单验证就可以了,下面我们介绍在控制器里使用这些表单

本作品采用《CC 协议》,转载必须注明作者和本文链接
MARTINPOTTER
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!