thinkphp验证规则

验证规则详解:

/**
 * Class ValidateRule
 * @package think\validate
 * @method ValidateRule confirm(mixed $rule, string $msg = '') static 验证是否和某个字段的值一致
 * @method ValidateRule different(mixed $rule, string $msg = '') static 验证是否和某个字段的值是否不同
 * @method ValidateRule egt(mixed $rule, string $msg = '') static 验证是否大于等于某个值
 * @method ValidateRule gt(mixed $rule, string $msg = '') static 验证是否大于某个值
 * @method ValidateRule elt(mixed $rule, string $msg = '') static 验证是否小于等于某个值
 * @method ValidateRule lt(mixed $rule, string $msg = '') static 验证是否小于某个值
 * @method ValidateRule eg(mixed $rule, string $msg = '') static 验证是否等于某个值
 * @method ValidateRule in(mixed $rule, string $msg = '') static 验证是否在范围内
 * @method ValidateRule notIn(mixed $rule, string $msg = '') static 验证是否不在某个范围
 * @method ValidateRule between(mixed $rule, string $msg = '') static 验证是否在某个区间
 * @method ValidateRule notBetween(mixed $rule, string $msg = '') static 验证是否不在某个区间
 * @method ValidateRule length(mixed $rule, string $msg = '') static 验证数据长度
 * @method ValidateRule max(mixed $rule, string $msg = '') static 验证数据最大长度
 * @method ValidateRule min(mixed $rule, string $msg = '') static 验证数据最小长度
 * @method ValidateRule after(mixed $rule, string $msg = '') static 验证日期
 * @method ValidateRule before(mixed $rule, string $msg = '') static 验证日期
 * @method ValidateRule expire(mixed $rule, string $msg = '') static 验证有效期
 * @method ValidateRule allowIp(mixed $rule, string $msg = '') static 验证IP许可
 * @method ValidateRule denyIp(mixed $rule, string $msg = '') static 验证IP禁用
 * @method ValidateRule regex(mixed $rule, string $msg = '') static 使用正则验证数据
 * @method ValidateRule token(mixed $rule='__token__', string $msg = '') static 验证表单令牌
 * @method ValidateRule is(mixed $rule, string $msg = '') static 验证字段值是否为有效格式
 * @method ValidateRule isRequire(mixed $rule = null, string $msg = '') static 验证字段必须
 * @method ValidateRule isNumber(mixed $rule = null, string $msg = '') static 验证字段值是否为数字
 * @method ValidateRule isArray(mixed $rule = null, string $msg = '') static 验证字段值是否为数组
 * @method ValidateRule isInteger(mixed $rule = null, string $msg = '') static 验证字段值是否为整形
 * @method ValidateRule isFloat(mixed $rule = null, string $msg = '') static 验证字段值是否为浮点数
 * @method ValidateRule isMobile(mixed $rule = null, string $msg = '') static 验证字段值是否为手机
 * @method ValidateRule isIdCard(mixed $rule = null, string $msg = '') static 验证字段值是否为身份证号码
 * @method ValidateRule isChs(mixed $rule = null, string $msg = '') static 验证字段值是否为中文
 * @method ValidateRule isChsDash(mixed $rule = null, string $msg = '') static 验证字段值是否为中文字母及下划线
 * @method ValidateRule isChsAlpha(mixed $rule = null, string $msg = '') static 验证字段值是否为中文和字母
 * @method ValidateRule isChsAlphaNum(mixed $rule = null, string $msg = '') static 验证字段值是否为中文字母和数字
 * @method ValidateRule isDate(mixed $rule = null, string $msg = '') static 验证字段值是否为有效格式
 * @method ValidateRule isBool(mixed $rule = null, string $msg = '') static 验证字段值是否为布尔值
 * @method ValidateRule isAlpha(mixed $rule = null, string $msg = '') static 验证字段值是否为字母
 * @method ValidateRule isAlphaDash(mixed $rule = null, string $msg = '') static 验证字段值是否为字母和下划线
 * @method ValidateRule isAlphaNum(mixed $rule = null, string $msg = '') static 验证字段值是否为字母和数字
 * @method ValidateRule isAccepted(mixed $rule = null, string $msg = '') static 验证字段值是否为yes, on, 或是 1
 * @method ValidateRule isEmail(mixed $rule = null, string $msg = '') static 验证字段值是否为有效邮箱格式
 * @method ValidateRule isUrl(mixed $rule = null, string $msg = '') static 验证字段值是否为有效URL地址
 * @method ValidateRule activeUrl(mixed $rule, string $msg = '') static 验证是否为合格的域名或者IP
 * @method ValidateRule ip(mixed $rule, string $msg = '') static 验证是否有效IP
 * @method ValidateRule fileExt(mixed $rule, string $msg = '') static 验证文件后缀
 * @method ValidateRule fileMime(mixed $rule, string $msg = '') static 验证文件类型
 * @method ValidateRule fileSize(mixed $rule, string $msg = '') static 验证文件大小
 * @method ValidateRule image(mixed $rule, string $msg = '') static 验证图像文件
 * @method ValidateRule method(mixed $rule, string $msg = '') static 验证请求类型
 * @method ValidateRule dateFormat(mixed $rule, string $msg = '') static 验证时间和日期是否符合指定格式
 * @method ValidateRule unique(mixed $rule, string $msg = '') static 验证是否唯一
 * @method ValidateRule behavior(mixed $rule, string $msg = '') static 使用行为类验证
 * @method ValidateRule filter(mixed $rule, string $msg = '') static 使用filter_var方式验证
 * @method ValidateRule requireIf(mixed $rule, string $msg = '') static 验证某个字段等于某个值的时候必须
 * @method ValidateRule requireCallback(mixed $rule, string $msg = '') static 通过回调方法验证某个字段是否必须
 * @method ValidateRule requireWith(mixed $rule, string $msg = '') static 验证某个字段有值的情况下必须
 * @method ValidateRule must(mixed $rule = null, string $msg = '') static 必须验证

编写验证规则:

<?php
declare (strict_types = 1);
namespace app\validate;
use think\Validate;

class Relation extends Validate {
    protected $rule = [
        'goods_name'  =>  'require|max:25',
        'goods_stock' =>  'require|number|egt:0',
        'goods_price' =>  'require|float|egt:0',
        'zipcode' => 'require|regex:\d{6}',  //邮政编码
        'city' => 'require|check_city',  //省市县
    ];

    protected $message  =   [
        'goods_name.require'  => '商品名称不能为空或者不能大于25个字符',
        'goods_stock.require' => '商品库存不能为空',
        'goods_stock.number'  => '商品库存只能是大于0的整数',
        'goods_stock.egt'     => '商品库存不能小于0',
        'goods_price.require' => '商品价格不能为空',
        'goods_price.float'   => '商品价格只能为数字',
        'goods_price.egt'     => '商品价格不能小于0',
        'zipcode.require' => '邮政编码不能为空',
        'zipcode.regex' => '邮政编码格式错误',
        'city.require' => '省市县不能为空',
        'city.check_city' => '省市县格式错误',
    ];

    protected $scene = [
        'add'  =>  ['goods_name','goods_stock','goods_price','zipcode','city'],
        'edit'  =>  ['goods_name','goods_stock','zipcode','city'],
    ];

    public function check_city($value)
    {
      
        return true;
    }

}



?>

控制器中调用验证规则

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use think\Request;
use think\response\Json;
use app\validate\Relation as relationValidate;
class relation extends BaseController{

    //验证器
   public function index() : Json {
       $data = Request()->param();
       $relation = new relationValidate();
       if (!$relation->scene("edit")->check($data)) {
           return json(['code'=>'1','msg'=>$relation->getError()]);
       }

       return json(['code'=>'0','msg'=>'验证成功','data'=>$data]);
   }

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值