| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Module\Farm\Validations;
- use App\Module\Farm\Validators\LandOwnershipValidator;
- use App\Module\Farm\Validators\RemovableStatusValidator;
- use App\Module\Farm\Validators\RemoveToolValidator;
- use UCore\ValidationCore;
- /**
- * 作物铲除验证类
- *
- * 用于验证作物铲除操作的输入数据,包括用户ID、土地ID和工具ID
- */
- class CropRemoveValidation extends ValidationCore
- {
- /** @var \App\Module\Farm\Models\FarmLand|null 土地对象,由 LandOwnershipValidator 设置 */
- public ?\App\Module\Farm\Models\FarmLand $land = null;
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,land_id', 'required'
- ],
- [
- 'user_id,land_id', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- [
- 'tool_item_id', 'integer', 'min' => 0,
- 'msg' => '{attr}必须是大于等于0的整数'
- ],
- // 验证土地是否属于用户
- [
- 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
- 'msg' => '土地不存在或不属于当前用户'
- ],
- // 验证土地状态是否可铲除
- [
- 'land_id', new RemovableStatusValidator($this, ['land']),
- 'msg' => '土地状态不允许铲除作物'
- ],
- // 验证铲除工具(如果提供)
- [
- 'tool_item_id', new RemoveToolValidator($this, ['user_id']),
- 'msg' => '铲除工具验证失败'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [
- 'tool_item_id' => 0
- ];
- }
- }
|