You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.3 KiB

<?php
namespace App\Http\Requests;
use App\Models\Study;
use Illuminate\Foundation\Http\FormRequest;
class StudyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => 'required|string|max:200',
'expire_day' => 'required|integer|min:1|max:365',
'minute' => 'required|integer|min:1|max:1440',
'rate' => 'required|numeric|min:0|max:100',
'content' => 'required|string',
'file' => 'nullable|array',
'type' => 'required|in:1,2,3',
];
}
/**
* Get custom messages for validator errors.
*/
public function messages(): array
{
return [
'name.required' => __('validation.required', ['attribute' => __('study.name')]),
'name.max' => __('validation.max.string', ['attribute' => __('study.name'), 'max' => 200]),
'expire_day.required' => __('validation.required', ['attribute' => __('study.expire_day')]),
'expire_day.integer' => __('validation.integer', ['attribute' => __('study.expire_day')]),
'expire_day.min' => __('validation.min.numeric', ['attribute' => __('study.expire_day'), 'min' => 1]),
'expire_day.max' => __('validation.max.numeric', ['attribute' => __('study.expire_day'), 'max' => 365]),
'minute.required' => __('validation.required', ['attribute' => __('study.minute')]),
'minute.integer' => __('validation.integer', ['attribute' => __('study.minute')]),
'minute.min' => __('validation.min.numeric', ['attribute' => __('study.minute'), 'min' => 1]),
'minute.max' => __('validation.max.numeric', ['attribute' => __('study.minute'), 'max' => 1440]),
'rate.required' => __('validation.required', ['attribute' => __('study.rate')]),
'rate.numeric' => __('validation.numeric', ['attribute' => __('study.rate')]),
'rate.min' => __('validation.min.numeric', ['attribute' => __('study.rate'), 'min' => 0]),
'rate.max' => __('validation.max.numeric', ['attribute' => __('study.rate'), 'max' => 100]),
'content.required' => __('validation.required', ['attribute' => __('study.content')]),
'file.array' => __('validation.array', ['attribute' => __('study.file')]),
'type.required' => __('validation.required', ['attribute' => __('study.type')]),
'type.in' => __('validation.in', ['attribute' => __('study.type')]),
];
}
/**
* Configure the validator instance.
*/
public function withValidator($validator): void
{
$validator->after(function ($validator) {
// 检查学习内容是否完整
if ($this->has('content') && empty(trim($this->input('content')))) {
$validator->errors()->add('content', __('study.content_cannot_be_empty'));
}
// 检查通过率是否合理
if ($this->has('rate') && $this->input('rate') < 60) {
$validator->errors()->add('rate', __('study.rate_too_low'));
}
});
}
}