dev
Jing Li 2023-10-26 20:31:59 +08:00
parent a3cd26602f
commit be80b4bf0c
1 changed files with 51 additions and 35 deletions

View File

@ -17,46 +17,62 @@ class AgriculturalBaseRequest extends FormRequest
*/ */
public function rules() public function rules()
{ {
if ($this->input('type') == 2) {
return [
'type' => ['bail', 'required', new Enum(BaseType::class)],
'name' => ['bail', 'required', 'string', 'max:100'],
'address' => ['bail', 'required', 'string', 'max:190'],
'areas' => ['bail', 'required', 'numeric', 'min:0', 'max:9999999999.99', 'regex:/^([1-9]\d*|0)(\.\d{1,2})?$/'],
'cultivated' => ['bail', 'required', 'numeric', 'min:0', 'max:9999999999.99', 'regex:/^([1-9]\d*|0)(\.\d{1,2})?$/'],
'workforce' => ['bail', 'required', 'int', 'min:0', 'max:2147483647'],
'description' => ['bail', 'nullable', 'string'],
];
}
return [ return [
'type' => ['required', new Enum(BaseType::class)], 'type' => ['bail', 'required', new Enum(BaseType::class)],
'name' => 'required|string|max:100', 'parent_id' => ['bail', 'required', 'int'],
'description' => 'nullable|string', 'name' => ['bail', 'required', 'string', 'max:100'],
'person' => 'required_if:type,1|string|max:100', 'person' => ['bail', 'required', 'string', 'max:100'],
'crops_ids' => 'nullable|array|min:1', 'crops_ids' => ['bail', 'required', 'array'],
'areas' => 'required|regex:/^\d+(\.\d{1,2})?$/', 'areas' => ['bail', 'required', 'numeric', 'min:0', 'max:9999999999.99', 'regex:/^([1-9]\d*|0)(\.\d{1,2})?$/'],
'workforce' => 'required|integer|min:0', 'cultivated' => ['bail', 'required', 'numeric', 'min:0', 'max:9999999999.99', 'regex:/^([1-9]\d*|0)(\.\d{1,2})?$/'],
'address' => 'nullable|string', 'workforce' => ['bail', 'required', 'int', 'min:0', 'max:2147483647'],
'address_lat' => 'nullable|regex:/^\d+(\.\d{1,10})?$/', 'description' => ['bail', 'nullable', 'string'],
'address_lng' => 'nullable|regex:/^\d+(\.\d{1,10})?$/', 'address' => ['bail', 'required', 'string', 'max:190'],
'parent_id' => 'required_if:type,1|integer|min:0', 'address_lat' => ['bail', 'nullable', 'regex:/^\d+(\.\d{1,10})?$/'],
'sort' => 'nullable|integer|min:0', 'address_lng' => ['bail', 'nullable', 'regex:/^\d+(\.\d{1,10})?$/'],
'sort' => ['bail', 'nullable', 'int', 'min:0', 'max:2147483647'],
]; ];
} }
public function messages() public function attributes()
{ {
$messages = [ if ($this->input('type') == 2) {
'type' => '请选择区域类别', return [
'name.required' => '请填写名称', 'name' => '街镇名称',
'name.max' => '名称不能超过100字', 'address' => '街镇地址',
'description.string' => '请正确填写介绍', 'areas' => '街镇面积',
'person.required_if' => '请填写负责人名称', 'cultivated' => '耕地面积',
'person.max' => '负责人名称不能超过100字', 'workforce' => '街镇人数',
'crops_ids.min' => '至少选择一种农作物', 'description' => '街镇介绍',
'areas.required' => '请填写面积', ];
'areas.regex' => '请正确填写面积', }
'workforce.required' => '请填写人数',
'workforce.min' => '人数最小为0', return [
'address.string' => '请正确填写地址', 'industry_key' => '农业类型',
'parent_id.required_if' => '请选择城镇', 'name' => '基地名称',
'person' => '基地负责人',
'areas' => '基地面积',
'cultivated' => '种养植面积',
'workforce' => '基地人数',
'parent_id' => '城镇',
'crops_ids' => '基地农作物',
'description' => '基地介绍',
'address' => '基地地址',
'address_lng' => '基地经度',
'address_lat' => '基地纬度',
'sort' => '排序',
]; ];
return $messages;
}
protected function failedValidation(Validator $validator)
{
$error = $validator->errors()->all();
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
} }
} }