DisasterRemovalValidation.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Module\Farm\Validations;
  3. use App\Module\Farm\Validators\LandOwnershipValidator;
  4. use App\Module\Farm\Validators\DisasterRemovalItemValidator;
  5. use UCore\ValidationCore;
  6. /**
  7. * 灾害去除验证类
  8. *
  9. * 用于验证灾害去除操作的输入数据,包括用户ID、土地ID、物品ID和灾害类型
  10. */
  11. class DisasterRemovalValidation extends ValidationCore
  12. {
  13. /** @var \App\Module\Farm\Models\FarmLand|null 土地对象,由 LandOwnershipValidator 设置 */
  14. public ?\App\Module\Farm\Models\FarmLand $land = null;
  15. /**
  16. * 验证规则
  17. *
  18. * @param array $rules 自定义规则
  19. * @return array
  20. */
  21. public function rules($rules = []): array
  22. {
  23. return [
  24. [
  25. 'user_id,land_id,item_id,disaster_type', 'required'
  26. ],
  27. [
  28. 'user_id,land_id,item_id,disaster_type', 'integer', 'min' => 1,
  29. 'msg' => '{attr}必须是大于0的整数'
  30. ],
  31. // 验证土地是否属于用户
  32. [
  33. 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
  34. 'msg' => '土地不存在或不属于当前用户'
  35. ],
  36. // 验证物品是否为对应的灾害去除道具
  37. [
  38. 'item_id', new DisasterRemovalItemValidator($this, ['user_id', 'disaster_type']),
  39. 'msg' => '物品验证失败'
  40. ]
  41. ];
  42. }
  43. /**
  44. * 设置默认值
  45. *
  46. * @return array
  47. */
  48. public function default(): array
  49. {
  50. return [];
  51. }
  52. }