Cate.php 1.1 KB

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