大白话:在添加一个用户时,要输入名称(name)、类别(type),不同的type值时,名称可以重复,否则不能。
class ClientStateList extends Validate { protected $rule = [ 'name' => 'checkName', ]; protected $message = [ 'name.checkName' => '名称重复,请修改' ]; /** * @author vancens's a.qiang * @title 验证name字段是否唯一(在同等company_id字段情况下) * @time 2019/4/2 5:25 * @param $value * @param $rules * @param array $data * @return bool * @throws hinkdbexceptionDataNotFoundException * @throws hinkdbexceptionModelNotFoundException * @throws hinkexceptionDbException */ protected function checkName($value,$rules,$data=[]){ //如果存在id,为修改,否则为新增 if (array_key_exists('id',$data)){ $mod = new ppdminmodelClientStateList(); $ret = $mod ->where(['name'=>$value,'company_id'=>$data['company_id']]) ->where('id','<>',$data['id']) ->find(); }else{ $mod = new ppdminmodelClientStateList(); $ret = $mod->where(['name'=>$value,'company_id'=>$data['company_id']])->find(); } if ($ret){ return false; }else{ return true; } } }