thinkphp(实例化验证器)

php验证


 // 实例化验证器
            $validate = Loader::validate('Business');
            // 验证数据
            $data     = ['business_name'=>$data['business_name'],'email'=>$data['email'],'contact_phone'=> $data['contact_phone'],'coin_type'=>$data['coin_type'],'category_name'=>$data['category_name']];
            // 验证
            if (!$validate->scene('edit')->check($data)) {
                return $this->error($validate->getError());
             }

类验证器

<?php
// +----------------------------------------------------------------------
// | Minishop [ Easy to handle for Micro businesses]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.qasl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: tangtanglove <dai_hang_love@126.com> <http://www.ixiaoquan.com>
// +----------------------------------------------------------------------

namespace app\admin\validate;

use think\Validate;
use think\Lang;

class Business extends Validate
{
    protected $rule =   [
        'business_name'       => 'require',
        'category_name'       => 'require',
        'email'          => 'email',             
        'coin_type'       => 'require',
        'business_address'     => 'require',
        'contact_phone'     => 'require',
        'longitude'     => 'require',
    ];

    protected $message  =   [
        'business_name.require '               => '商户名称不能为空',
        'category_name.require '               => '商户类别 ',
        'email.require'                           => '邮箱格式错误',
        'coin_type.require'                           => '币种不能为空',
        'business_address.require'              => '商户地址不能为空',
        'contact_phone.require'              => '联系人手机不能为空',
        'longitude.require'              => '经度不能为空',
    ];

    protected $scene = [
        'edit'                 =>  ['email','business_name','mobile','category_name','coin_type'],
        'add'             =>  ['password','repassword','confirmpassword'],
    ];
}

result= validate->scene(‘edit’)->check($data);

表示验证edit场景(该场景定义只需要验证name和age字段)。
如果使用了验证器,可以直接在类里面定义场景,例如:

然后再需要验证的地方直接使用 scene 方法验证
http://www.kancloud.cn/manual/thinkphp5/129322

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值