CropRemoveValidation.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /** @var \App\Module\Farm\Models\FarmLand|null 土地对象,由 LandOwnershipValidator 设置 */
  15. public ?\App\Module\Farm\Models\FarmLand $land = null;
  16. /**
  17. * 验证规则
  18. *
  19. * @param array $rules 自定义规则
  20. * @return array
  21. */
  22. public function rules($rules = []): array
  23. {
  24. return [
  25. [
  26. 'user_id,land_id', 'required'
  27. ],
  28. [
  29. 'user_id,land_id', 'integer', 'min' => 1,
  30. 'msg' => '{attr}必须是大于0的整数'
  31. ],
  32. [
  33. 'tool_item_id', 'integer', 'min' => 0,
  34. 'msg' => '{attr}必须是大于等于0的整数'
  35. ],
  36. // 验证土地是否属于用户
  37. [
  38. 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
  39. 'msg' => '土地不存在或不属于当前用户'
  40. ],
  41. // 验证土地状态是否可铲除
  42. [
  43. 'land_id', new RemovableStatusValidator($this, ['land']),
  44. 'msg' => '土地状态不允许铲除作物'
  45. ],
  46. // 验证铲除工具(如果提供)
  47. [
  48. 'tool_item_id', new RemoveToolValidator($this, ['user_id']),
  49. 'msg' => '铲除工具验证失败'
  50. ]
  51. ];
  52. }
  53. /**
  54. * 设置默认值
  55. *
  56. * @return array
  57. */
  58. public function default(): array
  59. {
  60. return [
  61. 'tool_item_id' => 0
  62. ];
  63. }
  64. }