<?php namespace app\common\validate; use think\Db; use think\Validate; class AttendanceMachine extends Validate{ protected $rule = [ 'sn|设备编号' => 'require|max:30|checkUnique', // 'org_id|所属项目' => 'require|gt:0', ]; protected function checkUnique($value,$rule,$data=[]) { $info = Db::name('attendance_machine') ->where('sn',$value) ->where('del',0) ->find(); if($data['id'] <= 0 && $info){ return '设备编号已被使用'; } if($info && $data['id'] > 0 && $info['id'] != $data['id']){ return '设备编号已被使用'; } return true; } }