DictDataValidate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\validate\dict;
  15. use app\common\model\dict\DictData;
  16. use app\common\model\dict\DictType;
  17. use app\common\validate\BaseValidate;
  18. /**
  19. * 字典数据验证
  20. * Class DictDataValidate
  21. * @package app\adminapi\validate\dict
  22. */
  23. class DictDataValidate extends BaseValidate
  24. {
  25. protected $rule = [
  26. 'id' => 'require|checkDictData',
  27. 'name' => 'require|length:1,255',
  28. 'value' => 'require',
  29. 'type_id' => 'require|checkDictType',
  30. 'status' => 'require|in:0,1',
  31. ];
  32. protected $message = [
  33. 'id.require' => '参数缺失',
  34. 'name.require' => '请填写字典数据名称',
  35. 'name.length' => '字典数据名称长度须在1-255位字符',
  36. 'value.require' => '请填写字典数据值',
  37. 'type_id.require' => '字典类型缺失',
  38. 'status.require' => '请选择字典数据状态',
  39. 'status.in' => '字典数据状态参数错误',
  40. ];
  41. /**
  42. * @notes 添加场景
  43. * @return DictDataValidate
  44. * @author 段誉
  45. * @date 2022/6/20 16:54
  46. */
  47. public function sceneAdd()
  48. {
  49. return $this->remove('id', true);
  50. }
  51. /**
  52. * @notes ID场景
  53. * @return DictDataValidate
  54. * @author 段誉
  55. * @date 2022/6/20 16:54
  56. */
  57. public function sceneId()
  58. {
  59. return $this->only(['id']);
  60. }
  61. /**
  62. * @notes 编辑场景
  63. * @return DictDataValidate
  64. * @author 段誉
  65. * @date 2022/6/20 18:36
  66. */
  67. public function sceneEdit()
  68. {
  69. return $this->remove('type_id', true);
  70. }
  71. /**
  72. * @notes 校验字典数据
  73. * @param $value
  74. * @return bool|string
  75. * @author 段誉
  76. * @date 2022/6/20 16:55
  77. */
  78. protected function checkDictData($value)
  79. {
  80. $article = DictData::findOrEmpty($value);
  81. if ($article->isEmpty()) {
  82. return '字典数据不存在';
  83. }
  84. return true;
  85. }
  86. /**
  87. * @notes 校验字典类型
  88. * @param $value
  89. * @return bool|string
  90. * @author 段誉
  91. * @date 2022/6/20 17:03
  92. */
  93. protected function checkDictType($value)
  94. {
  95. $type = DictType::findOrEmpty($value);
  96. if ($type->isEmpty()) {
  97. return '字典类型不存在';
  98. }
  99. return true;
  100. }
  101. }