123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace app\common\validate;
- use think\Db;
- use think\Validate;
- class Program extends Validate{
- protected $rule = [
- 'title|名称' => 'require|length:1,100|checkUnique',
- 'sn|标识' => 'require|length:1,5|alpha|upper|checkUniqueSn',
- 'company_id|公司' => 'require',
- ];
- protected $message = [
- 'company_id.require' => '未选择公司',
- 'title.length' => '名称必须100字以内',
- 'sn.length' => '标识必须是1-5个大写字母',
- 'sn.alpha' => '标识必须是1-5个大写字母',
- 'sn.upper' => '标识必须是1-5个大写字母',
- ];
- protected $scene = [
- ];
- // 自定义验证规则
- protected function checkUnique($value,$rule,$data=[])
- {
- $info = Db::name('program')->where('title',$value)->where('org_id',$data['org_id'])->where('del',0)->find();
- if($data['id'] <= 0 && $info){
- return '名称已被使用';
- }
- if($info && $data['id'] > 0 && $info['id'] != $data['id']){
- return '名称已被使用';
- }
- return true;
- }
- protected function checkUniqueSn($value,$rule,$data=[])
- {
- $info = Db::name('program')
- ->where('sn',$value)
- ->where('org_id',$data['org_id'])
- ->where('del',0)
- ->find();
- if($data['id'] <= 0 && $info){
- return '标识已被使用';
- }
- if($info && $data['id'] > 0 && $info['id'] != $data['id']){
- return '标识已被使用';
- }
- return true;
- }
- }
|