BudgetItems.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace app\common\validate;
  3. use think\Db;
  4. use think\Validate;
  5. class BudgetItems extends Validate{
  6. protected $rule = [
  7. 'title|名称' => 'require|checkUnique',
  8. // 'budget_id' => 'require|gt:0',
  9. 'company_id' => 'require|gt:0',
  10. ];
  11. protected $message = [
  12. // 'budget_id.require' => '参数错误',
  13. // 'budget_id.gt' => '参数错误',
  14. 'company_id.require' => '参数错误',
  15. 'company_id.gt' => '参数错误',
  16. ];
  17. protected $scene = [
  18. ];
  19. protected function checkUnique($value,$rule,$data=[])
  20. {
  21. $info = Db::name('budget_items')
  22. ->where('title',$data['title'])
  23. // ->where('budget_id',$data['budget_id'])
  24. ->where('company_id',$data['company_id'])
  25. ->find();
  26. if($data['id'] <= 0 && $info){
  27. return '名称已被占用';
  28. }
  29. if($info && $data['id'] > 0 && $info['id'] != $data['id']){
  30. return '名称已被占用';
  31. }
  32. return true;
  33. }
  34. }