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.
99 lines
3.8 KiB
99 lines
3.8 KiB
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\StudyAsk;
|
|
use App\Models\Study;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class StudyAskRequest 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 [
|
|
'study_id' => 'required|exists:studies,id',
|
|
'title' => 'required|string|max:500',
|
|
'type' => 'required|in:1,2',
|
|
'answer' => 'required|array|min:2',
|
|
'answer.*.content' => 'required|string|max:200',
|
|
'answer.*.result' => 'required|in:0,1',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'study_id.required' => __('validation.required', ['attribute' => __('study_ask.study_id')]),
|
|
'study_id.exists' => __('validation.exists', ['attribute' => __('study_ask.study_id')]),
|
|
'title.required' => __('validation.required', ['attribute' => __('study_ask.title')]),
|
|
'title.max' => __('validation.max.string', ['attribute' => __('study_ask.title'), 'max' => 500]),
|
|
'type.required' => __('validation.required', ['attribute' => __('study_ask.type')]),
|
|
'type.in' => __('validation.in', ['attribute' => __('study_ask.type')]),
|
|
'answer.required' => __('validation.required', ['attribute' => __('study_ask.answer')]),
|
|
'answer.array' => __('validation.array', ['attribute' => __('study_ask.answer')]),
|
|
'answer.min' => __('validation.min.array', ['attribute' => __('study_ask.answer'), 'min' => 2]),
|
|
'answer.*.content.required' => __('validation.required', ['attribute' => __('study_ask.answer_content')]),
|
|
'answer.*.content.max' => __('validation.max.string', ['attribute' => __('study_ask.answer_content'), 'max' => 200]),
|
|
'answer.*.result.required' => __('validation.required', ['attribute' => __('study_ask.answer_result')]),
|
|
'answer.*.result.in' => __('validation.in', ['attribute' => __('study_ask.answer_result')]),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Configure the validator instance.
|
|
*/
|
|
public function withValidator($validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
// 检查学习内容是否存在
|
|
if ($this->has('study_id')) {
|
|
$study = Study::find($this->input('study_id'));
|
|
if (!$study) {
|
|
$validator->errors()->add('study_id', __('study.not_found'));
|
|
}
|
|
}
|
|
|
|
// 检查答案选项
|
|
if ($this->has('answer')) {
|
|
$answers = $this->input('answer');
|
|
$correctCount = 0;
|
|
|
|
foreach ($answers as $index => $answer) {
|
|
if (isset($answer['result']) && $answer['result'] == 1) {
|
|
$correctCount++;
|
|
}
|
|
}
|
|
|
|
// 检查是否有正确答案
|
|
if ($correctCount == 0) {
|
|
$validator->errors()->add('answer', __('study_ask.no_correct_answer'));
|
|
}
|
|
|
|
// 检查单选题是否只有一个正确答案
|
|
if ($this->input('type') == StudyAsk::TYPE_SINGLE && $correctCount > 1) {
|
|
$validator->errors()->add('answer', __('study_ask.single_choice_multiple_correct'));
|
|
}
|
|
|
|
// 检查多选题是否至少有一个正确答案
|
|
if ($this->input('type') == StudyAsk::TYPE_MULTIPLE && $correctCount < 1) {
|
|
$validator->errors()->add('answer', __('study_ask.multiple_choice_no_correct'));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|