银河源码

高品质源码点击这里给我发消息PHP-LotusAdmin官方论坛

thinkphp5验证器:复杂多场景验证 thinkphp5验证器实例

验证器 UserPay

<?php
namespace app\common\validate;

use app\common\model\UserPayModel;
use think\Validate;

class PayInfo extends Validate {
    protected $rule = [
        'pay_type' => 'require',
        'pay_name' => 'require|max:50',
        'pay_account' => 'require',
        'bank_org_name' => 'require',
        'bank_idcard' => 'require',
        'pay_mobile' => 'require',
    ];
    protected $field = [
        'pay_type' => '类型',
        'pay_name' => '姓名',
        'pay_account' => '账号',
        'bank_org_name' => '银行',
        'bank_idcard' => '身份证',
        'pay_mobile' => '手机号'
    ];
    protected  $message = [
        'pay_mobile.checkMobile'=>'手机号码格式错误'
    ];
    // 验证场景 'save' => ['phone','xxx'],
    protected $scene = [
        'wechat'=>[
            'pay_type',
            'pay_name',
            'pay_account'
        ],
        'alipay'=>[
            'pay_type',
            'pay_name',
            'pay_account'=>'require'
        ],
        'bank_card'=>[
            'pay_type',
            'pay_name',
            'pay_account'=>'require|checkBankAccount|number',
            'bank_org_name' => 'require',
            'bank_idcard' => 'require',
            'pay_mobile' => 'require|number|checkMobile',
        ]
    ];

}

控制器方法验证:

<?php
    $param = $this->request->post();
    //数据验证
    switch ($param['pay_type']){
        case 0:
            $res  =  $this->validate($param,'PayInfo.wechat');
            break;
        case 1:
            $res  =  $this->validate($param,'PayInfo.alipay');
            break;
        case 2:
            $res  =  $this->validate($param,'PayInfo.bank_card');
            break;
    }
    if(true !== $res){
        throw new Exception($res);
    }

如果支付遇到问题,请主动联系站长,站长QQ 610176732

当前页面:银河源码 » thinkphp5验证器:复杂多场景验证 thinkphp5验证器实例

评论