CleaningType.php 1.2 KB

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