route()->getActionMethod(); return match($action) { 'list' => $this->listRules(), 'detail' => $this->detailRules(), 'create' => $this->createRules(), 'update' => $this->updateRules(), 'delete' => $this->deleteRules(), 'batchDelete' => $this->batchDeleteRules(), 'updateStatus' => $this->updateStatusRules(), 'batchUpdateStatus' => $this->batchUpdateStatusRules(), 'updateSort' => $this->updateSortRules(), 'batchUpdateSort' => $this->batchUpdateSortRules(), 'getByType' => $this->getByTypeRules(), 'options' => $this->optionsRules(), 'getLabel' => $this->getLabelRules(), 'getByTypeAndValue' => $this->getByTypeAndValueRules(), 'statistics' => $this->statisticsRules(), 'simpleList' => $this->simpleListRules(), 'groupStatistics' => [], default => [] }; } /** * 列表查询验证规则 */ private function listRules(): array { return array_merge($this->getPaginationRules(), [ 'label' => ['sometimes', 'string', 'max:100'], 'value' => ['sometimes', 'string', 'max:100'], 'dict_type' => ['sometimes', 'string', 'max:100'], 'status' => ['sometimes', 'integer', 'in:0,1'], 'color_type' => ['sometimes', 'string', 'max:100'], 'create_time_start' => ['sometimes', 'date'], 'create_time_end' => ['sometimes', 'date'], ]); } /** * 详情查询验证规则 */ private function detailRules(): array { return [ 'id' => ['required', 'integer', 'exists:system_dict_data,id'], ]; } /** * 创建验证规则 */ private function createRules(): array { return [ 'sort' => ['sometimes', 'integer', 'min:0'], 'label' => ['required', 'string', 'max:100'], 'value' => ['required', 'string', 'max:100'], 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], 'status' => ['sometimes', 'integer', 'in:0,1'], 'color_type' => ['sometimes', 'nullable', 'string', 'max:100'], 'css_class' => ['sometimes', 'nullable', 'string', 'max:100'], 'remark' => ['sometimes', 'nullable', 'string', 'max:500'], ]; } /** * 更新验证规则 */ private function updateRules(): array { return [ 'id' => ['required', 'integer', 'exists:system_dict_data,id'], 'sort' => ['sometimes', 'integer', 'min:0'], 'label' => ['required', 'string', 'max:100'], 'value' => ['required', 'string', 'max:100'], 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], 'status' => ['sometimes', 'integer', 'in:0,1'], 'color_type' => ['sometimes', 'nullable', 'string', 'max:100'], 'css_class' => ['sometimes', 'nullable', 'string', 'max:100'], 'remark' => ['sometimes', 'nullable', 'string', 'max:500'], ]; } /** * 删除验证规则 */ private function deleteRules(): array { return [ 'id' => ['required', 'integer', 'exists:system_dict_data,id'], ]; } /** * 批量删除验证规则 */ private function batchDeleteRules(): array { return [ 'ids' => ['required', 'array', 'min:1'], 'ids.*' => ['integer', 'exists:system_dict_data,id'], ]; } /** * 更新状态验证规则 */ private function updateStatusRules(): array { return [ 'id' => ['required', 'integer', 'exists:system_dict_data,id'], 'status' => ['required', 'integer', 'in:0,1'], ]; } /** * 批量更新状态验证规则 */ private function batchUpdateStatusRules(): array { return [ 'ids' => ['required', 'array', 'min:1'], 'ids.*' => ['integer', 'exists:system_dict_data,id'], 'status' => ['required', 'integer', 'in:0,1'], ]; } /** * 更新排序验证规则 */ private function updateSortRules(): array { return [ 'id' => ['required', 'integer', 'exists:system_dict_data,id'], 'sort' => ['required', 'integer', 'min:0'], ]; } /** * 批量更新排序验证规则 */ private function batchUpdateSortRules(): array { return [ 'sort_data' => ['required', 'array', 'min:1'], 'sort_data.*.id' => ['required', 'integer', 'exists:system_dict_data,id'], 'sort_data.*.sort' => ['required', 'integer', 'min:0'], ]; } /** * 根据类型获取数据验证规则 */ private function getByTypeRules(): array { return [ 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], 'only_active' => ['sometimes', 'boolean'], ]; } /** * 获取选项验证规则 */ private function optionsRules(): array { return [ 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], 'only_active' => ['sometimes', 'boolean'], ]; } /** * 获取标签验证规则 */ private function getLabelRules(): array { return [ 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], 'value' => ['required', 'string', 'max:100'], ]; } /** * 根据类型和值获取数据验证规则 */ private function getByTypeAndValueRules(): array { return [ 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], 'value' => ['required', 'string', 'max:100'], ]; } /** * 统计信息验证规则 */ private function statisticsRules(): array { return [ 'dict_type' => ['sometimes', 'string', 'max:100', 'exists:system_dict_type,type'], ]; } /** * 简单列表验证规则 */ private function simpleListRules(): array { return [ 'dict_type' => ['required', 'string', 'max:100', 'exists:system_dict_type,type'], ]; } /** * 获取验证错误消息 */ public function messages(): array { return [ 'sort.integer' => '排序必须是整数', 'sort.min' => '排序不能小于0', 'sort.required' => '排序不能为空', 'label.required' => '字典标签不能为空', 'label.string' => '字典标签必须是字符串', 'label.max' => '字典标签最大长度为100个字符', 'value.required' => '字典键值不能为空', 'value.string' => '字典键值必须是字符串', 'value.max' => '字典键值最大长度为100个字符', 'dict_type.required' => '字典类型不能为空', 'dict_type.string' => '字典类型必须是字符串', 'dict_type.max' => '字典类型最大长度为100个字符', 'dict_type.exists' => '字典类型不存在', 'status.integer' => '状态必须是整数', 'status.in' => '状态值无效,只能是0或1', 'status.required' => '状态不能为空', 'color_type.string' => '颜色类型必须是字符串', 'color_type.max' => '颜色类型最大长度为100个字符', 'css_class.string' => 'CSS样式必须是字符串', 'css_class.max' => 'CSS样式最大长度为100个字符', 'remark.string' => '备注必须是字符串', 'remark.max' => '备注最大长度为500个字符', 'id.required' => 'ID不能为空', 'id.integer' => 'ID必须是整数', 'id.exists' => '字典数据不存在', 'ids.required' => '请选择要操作的字典数据', 'ids.array' => 'IDs必须是数组', 'ids.min' => '至少选择一条记录进行操作', 'ids.*.integer' => 'ID必须是整数', 'ids.*.exists' => '选中的字典数据不存在', 'sort_data.required' => '排序数据不能为空', 'sort_data.array' => '排序数据必须是数组', 'sort_data.min' => '排序数据不能为空', 'sort_data.*.id.required' => '排序数据ID不能为空', 'sort_data.*.id.integer' => '排序数据ID必须是整数', 'sort_data.*.id.exists' => '排序数据ID不存在', 'sort_data.*.sort.required' => '排序值不能为空', 'sort_data.*.sort.integer' => '排序值必须是整数', 'sort_data.*.sort.min' => '排序值不能小于0', 'only_active.boolean' => '只获取启用状态参数必须是布尔值', 'create_time_start.date' => '开始时间格式不正确', 'create_time_end.date' => '结束时间格式不正确', ]; } /** * 字段名称 * * @return array */ public function attributes(): array { return [ 'sort' => '排序', 'label' => '字典标签', 'value' => '字典键值', 'dict_type' => '字典类型', 'status' => '状态', 'color_type' => '颜色类型', 'css_class' => 'CSS样式', 'remark' => '备注', 'id' => 'ID', 'ids' => 'IDs', 'sort_data' => '排序数据', 'only_active' => '只获取启用状态', ]; } /** * 验证数据预处理 * * @return void */ protected function prepareForValidation(): void { // 设置默认状态 if (!$this->has('status') && $this->route()->getActionMethod() === 'create') { $this->merge(['status' => SystemDictData::STATUS_NORMAL]); } // 设置默认排序 if (!$this->has('sort') && $this->has('dict_type') && $this->route()->getActionMethod() === 'create') { $nextSort = SystemDictData::getNextSort($this->input('dict_type')); $this->merge(['sort' => $nextSort]); } // 移除空白字符 $trimFields = ['label', 'value', 'dict_type', 'color_type', 'css_class', 'remark']; foreach ($trimFields as $field) { if ($this->has($field)) { $this->merge([$field => trim($this->input($field))]); } } } /** * 自定义验证规则 * * @param \Illuminate\Validation\Validator $validator */ public function withValidator($validator): void { $validator->after(function ($validator) { // 验证字典类型是否启用 if ($this->has('dict_type')) { $dictType = SystemDictType::findByType($this->input('dict_type')); if ($dictType && $dictType->isDisabled()) { $validator->errors()->add('dict_type', '该字典类型已被停用'); } } // 验证字典值唯一性(创建或更新时) $action = $this->route()->getActionMethod(); if (in_array($action, ['create', 'update']) && $this->has('value') && $this->has('dict_type')) { $query = SystemDictData::where('value', $this->input('value')) ->where('dict_type', $this->input('dict_type')); if ($action === 'update' && $this->has('id')) { $query->where('id', '!=', $this->input('id')); } if ($query->exists()) { $validator->errors()->add('value', '该字典类型下的字典键值已存在'); } } }); } /** * 获取特定的验证规则(BaseRequest需要) * * @return array */ protected function getSpecificRules(): array { return []; } /** * 获取特定的验证消息(BaseRequest需要) * * @return array */ protected function getSpecificMessages(): array { return []; } /** * 获取分页验证规则 * * @return array */ private function getPaginationRules(): array { return [ 'page' => ['sometimes', 'integer', 'min:1'], 'page_size' => ['sometimes', 'integer', 'min:1', 'max:100'], ]; } /** * 获取验证失败后的错误重定向URL * * @return string */ protected function getRedirectUrl(): string { return $this->ajax() ? '' : url()->previous(); } }