0
0

Device.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\validate;
  4. use think\Db;
  5. use think\Validate;
  6. class Device extends Validate
  7. {
  8. /**
  9. * 定义验证规则
  10. * 格式:'字段名' => ['规则1','规则2'...]
  11. *
  12. * @var array
  13. */
  14. protected $rule = [
  15. 'title|设备名称' => 'require|length:1,20|checkUnique',
  16. 'imgs|设备图片' => 'require',
  17. 'enable|状态' => 'require',
  18. 'cate_id|分类' => 'require|gt:0',
  19. 'device_form|检查项' => 'require',
  20. 'content|设备详情' => 'require',
  21. ];
  22. /**
  23. * 定义错误信息
  24. * 格式:'字段名.规则名' => '错误信息'
  25. *
  26. * @var array
  27. */
  28. protected $message = [
  29. 'cate_id.require'=>'分类不能为空',
  30. 'cate_id.gt'=>'分类不能为空',
  31. ];
  32. protected function checkUnique($value, $rule, $data=[]){
  33. if(isset($data['id']) && $data['id'] > 0){
  34. $ret = Db::name('device')
  35. ->where('del',0)
  36. ->where('title',$data['title'])
  37. ->where('org_id',$data['org_id'])
  38. ->where('id','<>',$data['id'])
  39. ->find();
  40. }else{
  41. $ret = Db::name('device')
  42. ->where('del',0)
  43. ->where('title',$data['title'])
  44. ->where('org_id',$data['org_id'])
  45. ->find();
  46. }
  47. return $ret?'名称已被使用':true;
  48. }
  49. }