企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 筛选表单字段验证器 > 说明: 设置筛选表单字段验证器。在点击搜索按钮进行列表的搜索时,对预设验证规则的筛选表单字段数据进行验证。在设定的验证器中可以写字段自定义的验证方法,也可对筛选表单字段数据进行进一步处理(handleData操作)。默认筛选表单字段验证器为 `\yunj\Validate` 方法:`filterValidate($callable)` * 参数 **callable** (必须),例: ```php filterValidate(function(){ return TestValidate(); }); ``` * 返回值:需为 `\yunj\Validate`或其子类的实例对象 * 验证环境scene: * `count`:列表搜索时获取搜索数据总数的验证环境 在`count`环境的验证数据中存在固定参数,`$data["state"]`当前验证数据行所在状态栏,`$data["ids"]`当前选中数据行的id集合。 * `items`:列表搜索时获取搜索数据的验证环境 在`items`环境的验证数据中存在固定参数 `$data["state"]` 当前验证数据行所在状态栏,`$data["ids"]`当前选中数据行的id集合。 * `export`:列表搜索时导出数据的验证环境 在`items`环境的验证数据中存在固定参数 `$data["state"]` 当前验证数据行所在状态栏,`$data["ids"]`当前选中数据行的id集合。 > 示例: 搜索产品数据:`序列号(sn)`、`名称(name)`、`商户(mid)`... * 验证器 ```php namespace app\demo\validate\product; final class Lists extends \yunj\Validate { // 自定义验证方法,校验序列号格式(这里序列号定义为10位字母组合) protected function checkSn($value,$rule = "",$data){ if(!$value) return true; if(!preg_match("/^[a-zA-Z]{10}$/",$value)) return "序列号仅支持10位字母组合"; return true; } protected function handleData(array $raw_data, $scene): array { $data = $raw_data; switch ($scene) { case "count": // 固定参数:状态 $state = $data["state"]; // 固定参数:选中数据行id集合 $ids = $data["ids"]; // 校验商户有效性,并获取商户数据添加到验证数据里 $merchant = Db::name("merchant")->where("id","=",$data["mid"])->select(); if(!$merchant ) throw_error_json("商户数据错误"); $data["merchant_data"] = $merchant; break; case "items": ... break; case "export": ... break; } return $data; } } ``` * 表格构建器: ```php $builder=YT('general_example')->filter(function($state){ $filter=[ 'sn'=>['title'=>'序列号','verify'=>'checkSn'], 'name'=>['title'=>'名称','verify'=>'checkSn','desc'=>'只能输入汉字'], 'mid'=>[ 'title'=>'商户', 'type'=>'dropdown_search', 'verify'=>'positiveInteger', "multi"=>false, 'url'=>url('merchantOptions') ] ]; return $filter; })->filterValidate(function(){ return new \app\demo\validate\product\Lists(); })->count(function($filter){ // 获取展商数据 $merchant = $filter["merchant_data"]; // 业务处理... })->items(function($limit_start,$limit_length,$filter,$sort){ ... }); ```