GreenAddr.php 1.2 KB

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