AdminMenuValidate.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\validate;
  12. use think\Validate;
  13. use think\Db;
  14. class AdminMenuValidate extends Validate
  15. {
  16. protected $rule = [
  17. 'name' => 'require',
  18. 'app' => 'require',
  19. 'controller' => 'require',
  20. 'parent_id' => 'checkParentId',
  21. 'action' => 'require|unique:AdminMenu,app^controller^action',
  22. ];
  23. protected $message = [
  24. 'name.require' => '名称不能为空',
  25. 'app.require' => '应用不能为空',
  26. 'parent_id' => '超过了4级',
  27. 'controller.require' => '名称不能为空',
  28. 'action.require' => '名称不能为空',
  29. 'action.unique' => '同样的记录已经存在!',
  30. ];
  31. protected $scene = [
  32. 'add' => ['name', 'app', 'controller', 'action', 'parent_id'],
  33. 'edit' => ['name', 'app', 'controller', 'action', 'id', 'parent_id'],
  34. ];
  35. // 自定义验证规则
  36. protected function checkParentId($value)
  37. {
  38. $find = Db::name('AdminMenu')->where("id", $value)->value('parent_id');
  39. if ($find) {
  40. $find2 = Db::name('AdminMenu')->where("id", $find)->value('parent_id');
  41. if ($find2) {
  42. $find3 = Db::name('AdminMenu')->where("id", $find2)->value('parent_id');
  43. if ($find3) {
  44. return false;
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. }