QualityCate.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\validate;
  4. use think\Db;
  5. use think\Validate;
  6. class QualityCate extends Validate
  7. {
  8. /**
  9. * 定义验证规则
  10. * 格式:'字段名' => ['规则1','规则2'...]
  11. *
  12. * @var array
  13. */
  14. protected $rule = [
  15. 'title|名称' => 'require|length:1,20|checkUnique',
  16. ];
  17. /**
  18. * 定义错误信息
  19. * 格式:'字段名.规则名' => '错误信息'
  20. *
  21. * @var array
  22. */
  23. protected $message = [
  24. ];
  25. protected function checkUnique($value, $rule, $data=[]){
  26. if(isset($data['id']) && $data['id'] > 0){
  27. $ret = Db::name('quality_cate')
  28. ->where('del',0)
  29. ->where('title',$data['title'])
  30. ->where('org_id',$data['org_id'])
  31. ->where('pid',$data['pid'])
  32. ->where('id','<>',$data['id'])
  33. ->find();
  34. }else{
  35. $ret = Db::name('quality_cate')
  36. ->where('del',0)
  37. ->where('title',$data['title'])
  38. ->where('org_id',$data['org_id'])
  39. ->where('pid',$data['pid'])
  40. ->find();
  41. }
  42. return $ret?'名称已被使用':true;
  43. }
  44. }