laravel 实现Form 表单场景验证

前言:

laravel本身没有像TP5那样的form表单的场景验证,这里是我自己根据理解自己封装的实现场景验证。


废话不多说了,上代码:

一、创建一个类 Validate 类,因为他是公共使用的没有建在控制器下面:

这里写图片描述

首先要继承laravel 自带的验证类 use Illuminate\Support\Facades\Validator;

<?php
namespace  App\Validate;

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules;

class Validate extends Validator
{
    /***
     * 验证字段属性
     *
     * @param $all
     * @param $rules
     * @param bool $message
     * @return array|bool|string
     */
    protected $input;

    /**
     * 重写验证场景
     * @param $inputs
     * @param $scene
     * @return bool|string
     */
    public function check($inputs,$scene){
        $input = $this->getInput($inputs,$scene);
        $rules = $this->getRules($scene);
        $messages = $this->getMessage($rules);
        $validator = Validator::make($input, $rules,$messages);

        //返回错误信息
        if ($validator->fails()) {
           return $validator->errors()->first(); //返回错误信息
        }
        return false;
    }
    //获取验证数据
    public function getInput($inputs,$scene)
    {
        foreach ($this->scene[$scene] as $key=>$v){
            if (array_key_exists($v, $inputs)){
                $input[$v] = $inputs[$v];
            }
        }
        return $input;
    }

    /**
     * 获取验证规则
     * @param $scene
     * @return mixed
     */
    public function getRules($scene)
    {
        if ($this->scene[$scene]){
            foreach ($this->scene[$scene] as $field){
                if (array_key_exists($field, $this->rule)){
                    $rules[$field] = $this->rule[$field];
                }
            }
        }
        return $rules;
    }


    /***
     * 返回验证message
     * @return array
     */
    public function getMessage($rules){
       foreach ($rules as $key=>$v){
           $arr = explode('|',$v);
           foreach($arr as $k=>$val){
                if (strpos($val,':')){
                    unset($arr[$k]);
                    $arr[] = substr($val,0,strpos($val, ':'));
                }

           }
           foreach($arr as $value){
               if (array_key_exists($key.'.'.$value, $this->message)){
                   $message[$key.'.'.$value] = $this->message[$key.'.'.$value];
               }
            }
       }
       return $message;
    }
//所有的验证都可以写在这个数组里面
    protected $rule = [
        'name'=>'required|unique:gems_activities|max:255',
        'time'=>'required',
        'duration'=>'required',
        'location'=>'required',
    ];
    //所有的验证信息提示都可以写在这个数组里面
    protected $message = [
        'name.required' => '活动名称不能为空',
        'name.unique' => '活动名称已经存在',
        'time.max' => '活动名称不能超过255个字符',
        'time.required' => '活动时间不能为空',
        'duration.required' => '活动周期不能为空',
    ];

    protected $scene = [
        'addAct'   =>  ['name','time','duration','location'],
        'edit'  =>  ['email'],
    ];

}

二、使用方法在你的控制器里面先引入你的自定义的类 use App\Validate\Validate;

public function __construct(){
        $this->validate = new  Validate();
}

三、在你接收form表单数据的方法里面实现:

  public function addCustomActivities(Request $reequest){

        if ($err = $this->validate->check($reequest->all(),'addAct'))
        throw new BaseException($err,0); //将异常抛出
...
...
...
//编写验证之后的逻辑代码
    }

好了,就这么些啦,各位小伙伴们有什么更好的方法,希望多多交流分享一下…

猜你喜欢

转载自blog.csdn.net/qq_26282869/article/details/82490746
今日推荐