ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 工作表表头验证验证器 > 说明: 设置工作表表头验证验证器。在导入表单数据时,对预设验证规则的工作表表头字段数据进行验证。在设定的验证器中可以写字段自定义的验证方法,也可对工作表表头字段数据进行进一步处理(handleData操作)。默认筛选表单字段验证器为 `\yunj\Validate` 方法:`colsValidate($callable)` * 参数 **callable** (必须),例: ```php colsValidate(function(){ return TestValidate(); }); ``` * 返回值:需为 `\yunj\Validate`或其子类的实例对象 * 验证环境scene: * `row`:数据导入验证每行数据时的验证环境 在`row`环境的验证数据中存在固定参数 `$data["sheet"]` 当前验证数据行所在工作表。 > 示例: 导入产品数据:`序列号(sn)`、`名称(name)`、`商户(mid)`... * 验证器 ```php namespace app\demo\validate\product; final class Import extends \yunj\Validate { // 自定义验证方法,校验序列号唯一性 protected function snUnique($value,$rule = "",$data){ $isExist = Db::name("product")->where("sn","=",$value)->select(); if($isExist ) return "该序列号已存在"; return true; } protected function handleData(array $raw_data, $scene): array { $data = $raw_data; switch ($scene) { case "row": // 固定参数:工作表 $sheet = $data["sheet"]; // 校验商户有效性,并获取商户数据添加到验证数据里 $merchant = Db::name("merchant")->where("id","=",$data["mid"])->select(); if(!$merchant ) throw_error_json("商户数据错误"); $data["merchant_data"] = $merchant; break; } return $data; } } ``` * 导入构建器: ```php $builder=YI('general_example')->cols(function($sheet){ $cols=[ 'sn'=>['title'=>'序列号','verify'=>'require|snUnique'], 'name'=>['title'=>'名称','verify'=>'require|checkSn','desc'=>'只能输入汉字'], 'mid'=>['title'=>'商户','verify'=>'require|positiveInteger'] ]; return $cols; })->colsValidate(function(){ return new \app\demo\validate\product\Import(); })->row(function($sheet,$row_data){ // 获取展商数据 $merchant = $row_data["merchant_data"]; // 业务处理... }); ```