inhere / php-validate

Lightweight and feature-rich PHP validation and filtering library. Support scene grouping, pre-filtering, array checking, custom validators, custom messages. 轻量且功能丰富的PHP验证、过滤库。支持场景分组,前置过滤,数组检查,自定义验证器,自定义消息。

Home Page:https://inhere.github.io/php-validate/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

对数组子元素进行验证时验证结果返回异常

trising opened this issue · comments

验证规则:

[
       ['goods_id', 'array', 'list', 'msg' => '商品id数组为空或不合法'],
       ['goods_id.*', 'integer', 'msg' => '商品分类id必须是一串数字']
]

验证的数据:

[
     "goods_id" => [
             1144181460261978556, 114418146, 1144
     ]
]

返回结果:商品分类id必须是一串数字

这里规则写错了。

[
       ['goods_id', 'list', 'msg' => '商品id数组为空或不合法'],
       ['goods_id.*', 'each', 'integer', 'msg' => '商品分类id必须是一串数字']
]
  1. 只能写一个验证器 'array', 'list' 同时写也只有 array 生效了。

这里推荐写 list,list 已经是包含了array验证的,先是array才会是list

  1. 'goods_id.*' 取到的是个 int list 数组,直接对它验证 integer,是没法通过的

使用 each 验证数组里的每个值 为 integer

  1. 'goods_id.0', 'integer' 这样指明某一个值验证 int 是可以的。

这里规则写错了。

[
       ['goods_id', 'list', 'msg' => '商品id数组为空或不合法'],
       ['goods_id.*', 'each', 'integer', 'msg' => '商品分类id必须是一串数字']
]
  1. 只能写一个验证器 'array', 'list' 同时写也只有 array 生效了。

这里推荐写 list,list 已经是包含了array验证的,先是array才会是list

  1. 'goods_id.*' 取到的是个 int list 数组,直接对它验证 integer,是没法通过的

使用 each 验证数组里的每个值 为 integer

  1. 'goods_id.0', 'integer' 这样指明某一个值验证 int 是可以的。

sorry,我这边看文档没看仔细。已经ok了