1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- declare (strict_types = 1);
- namespace app\common\validate;
- use think\Db;
- use think\Validate;
- class Device extends Validate
- {
- /**
- * 定义验证规则
- * 格式:'字段名' => ['规则1','规则2'...]
- *
- * @var array
- */
- protected $rule = [
- 'title|设备名称' => 'require|length:1,20|checkUnique',
- 'imgs|设备图片' => 'require',
- 'enable|状态' => 'require',
- 'cate_id|分类' => 'require|gt:0',
- 'device_form|检查项' => 'require',
- 'content|设备详情' => 'require',
- ];
- /**
- * 定义错误信息
- * 格式:'字段名.规则名' => '错误信息'
- *
- * @var array
- */
- protected $message = [
- 'cate_id.require'=>'分类不能为空',
- 'cate_id.gt'=>'分类不能为空',
- ];
- protected function checkUnique($value, $rule, $data=[]){
- if(isset($data['id']) && $data['id'] > 0){
- $ret = Db::name('device')
- ->where('del',0)
- ->where('title',$data['title'])
- ->where('org_id',$data['org_id'])
- ->where('id','<>',$data['id'])
- ->find();
- }else{
- $ret = Db::name('device')
- ->where('del',0)
- ->where('title',$data['title'])
- ->where('org_id',$data['org_id'])
- ->find();
- }
- return $ret?'名称已被使用':true;
- }
- }
|