CropRemoveValidation.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Module\Farm\Validations;
  3. use App\Module\Farm\Validators\LandOwnershipValidator;
  4. use App\Module\Farm\Validators\RemovableStatusValidator;
  5. use App\Module\Farm\Validators\RemoveToolValidator;
  6. use UCore\ValidationCore;
  7. /**
  8. * 作物铲除验证类
  9. *
  10. * 用于验证作物铲除操作的输入数据,包括用户ID、土地ID和工具ID
  11. */
  12. class CropRemoveValidation extends ValidationCore
  13. {
  14. /**
  15. * 验证规则
  16. *
  17. * @param array $rules 自定义规则
  18. * @return array
  19. */
  20. public function rules($rules = []): array
  21. {
  22. return [
  23. [
  24. 'user_id,land_id', 'required'
  25. ],
  26. [
  27. 'user_id,land_id', 'integer', 'min' => 1,
  28. 'msg' => '{attr}必须是大于0的整数'
  29. ],
  30. [
  31. 'tool_item_id', 'integer', 'min' => 0,
  32. 'msg' => '{attr}必须是大于等于0的整数'
  33. ],
  34. // 验证土地是否属于用户
  35. [
  36. 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
  37. 'msg' => '土地不存在或不属于当前用户'
  38. ],
  39. // 验证土地状态是否可铲除
  40. [
  41. 'land_id', new RemovableStatusValidator($this, ['land']),
  42. 'msg' => '土地状态不允许铲除作物'
  43. ],
  44. // 验证铲除工具(如果提供)
  45. [
  46. 'tool_item_id', new RemoveToolValidator($this, ['user_id']),
  47. 'msg' => '铲除工具验证失败'
  48. ]
  49. ];
  50. }
  51. /**
  52. * 设置默认值
  53. *
  54. * @return array
  55. */
  56. public function default(): array
  57. {
  58. return [
  59. 'tool_item_id' => 0
  60. ];
  61. }
  62. }