ShopGoodsCate.php 1.0 KB

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