1
0

fix question validation

This commit is contained in:
Rokas Puzonas 2023-11-25 17:44:59 +02:00
parent f53f5914fc
commit 8a9007561f
3 changed files with 53 additions and 30 deletions

View File

@ -8,6 +8,7 @@ use App\Models\SingleChoiceAnswer;
use App\Models\SingleQuestionAnswers;
use App\Models\Test;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class QuestionController extends Controller
{
@ -32,45 +33,68 @@ class QuestionController extends Controller
*/
public function store(Request $request)
{
$validated = $request->validate([
$validation_rules = [
"question" => "required|string",
"points" => "required|integer|min:1",
"test_id" => "required|integer",
"answer_type" => "required|string",
]);
];
if ($request->answer_type == "single_answer") {
$validated_answer = $request->validate([
"answer" => "required|string",
]);
$validation_rules["answer"] = "required|string";
} else if ($request->answer_type == "single_choice") {
$validated_answer = $request->validate([
"correct_answer" => "required|string",
"incorrect_answer1" => "required|string",
"incorrect_answer2" => "nullable|string",
"incorrect_answer3" => "nullable|string",
]);
$validation_rules["correct_answer"] = "required|string";
$validation_rules["incorrect_answer1"] = "required|string";
$validation_rules["incorrect_answer2"] = "nullable|string";
$validation_rules["incorrect_answer3"] = "nullable|string";
} else if ($request->answer_type == "multiple_choice") {
$validated_answer = $request->validate([
"answer1" => "required|string",
"answer2" => "required|string",
"answer3" => "nullable|string",
"answer4" => "nullable|string",
"is_answer1_correct" => "nullable|boolean",
"is_answer2_correct" => "nullable|boolean",
"is_answer3_correct" => "nullable|boolean",
"is_answer4_correct" => "nullable|boolean",
]);
$validation_rules["answer1"] = "required|string";
$validation_rules["answer2"] = "required|string";
$validation_rules["answer3"] = "nullable|string";
$validation_rules["answer4"] = "nullable|string";
$validation_rules["is_answer1_correct"] = "nullable|string";
$validation_rules["is_answer2_correct"] = "nullable|string";
$validation_rules["is_answer3_correct"] = "nullable|string";
$validation_rules["is_answer4_correct"] = "nullable|string";
}
$question = Question::create($validated);
$validated_answer["question_id"] = $question->id;
$validated = $request->validate($validation_rules);
$question = Question::create([
"question" => $validated["question"],
"points" => $validated["points"],
"test_id" => $validated["test_id"],
"answer_type" => $validated["answer_type"],
]);
if ($request->answer_type == "single_answer") {
SingleQuestionAnswers::create($validated_answer);
SingleQuestionAnswers::create([
"question_id" => $question->id,
"answer" => $validated["answer"],
]);
} else if ($request->answer_type == "single_choice") {
SingleChoiceAnswer::create($validated_answer);
SingleChoiceAnswer::create([
"question_id" => $question->id,
"correct_answer" => $validated["correct_answer"],
"incorrect_answer1" => $validated["incorrect_answer1"],
"incorrect_answer2" => $validated["incorrect_answer2"],
"incorrect_answer3" => $validated["incorrect_answer3"],
]);
} else if ($request->answer_type == "multiple_choice") {
MultipleChoiceAnswers::create($validated_answer);
$validated["is_answer1_correct"] = !empty($validated["is_answer1_correct"]) && $validated["is_answer1_correct"] == "on";
$validated["is_answer2_correct"] = !empty($validated["is_answer2_correct"]) && $validated["is_answer2_correct"] == "on";
$validated["is_answer3_correct"] = !empty($validated["is_answer3_correct"]) && $validated["is_answer3_correct"] == "on";
$validated["is_answer4_correct"] = !empty($validated["is_answer4_correct"]) && $validated["is_answer4_correct"] == "on";
MultipleChoiceAnswers::create([
"question_id" => $question->id,
"answer1" => $validated["answer1"],
"answer2" => $validated["answer2"],
"answer3" => $validated["answer3"],
"answer4" => $validated["answer4"],
"is_answer1_correct" => $validated["is_answer1_correct"],
"is_answer2_correct" => $validated["is_answer2_correct"],
"is_answer3_correct" => $validated["is_answer3_correct"],
"is_answer4_correct" => $validated["is_answer4_correct"],
]);
}
return redirect(route("tests.edit", [

View File

@ -52,7 +52,7 @@
@for ($idx = 1; $idx <= 3; $idx++)
<x-input-label>{{ __("Incorrect answer ".$idx) }}</x-input-label>
<x-text-input class="w-full" name="incorrect_answer{{$idx}}" placeholder="{{ __('Incorrect answer '.$idx) }}" value="{{ old('incorrect_answer'.$idx, ${'default_incorrect_answer'.$idx}) }}" />
<x-input-error :messages="$errors->get('incorrect_answer{{$idx}}')" class="mt-2" />
<x-input-error :messages="$errors->get('incorrect_answer'.$idx)" class="mt-2" />
@endfor
</div>
@ -61,7 +61,7 @@
<div>
<x-input-label>{{ __("Answer ".$idx) }}</x-input-label>
<x-text-input class="w-full" name="answer{{ $idx }}" placeholder="{{ __('Answer '.$idx) }}" value="{{ old('answer'.$idx, ${'default_answer'.$idx}) }}" />
<x-input-error :messages="$errors->get('answer{{ $idx }}')" class="mt-2" />
<x-input-error :messages="$errors->get('answer'.$idx)" class="mt-2" />
<x-bool-input name="is_answer{{$idx}}_correct" checked="{{ old('is_answer'.$idx.'_correct', ${'default_is_answer'.$idx.'_correct'}) }}" />
</div>
@ -87,5 +87,5 @@
$("#answer_type").on("change", function() {
show(this.value)
})
show("{{ $default_answer_type }}")
show("{{ old('answer_type', $default_answer_type) }}")
</script>

View File

@ -4,7 +4,6 @@
@csrf
@method("patch")
<div class="flex flex-col gap-1">
{{ $question->answer_type }}
<x-question-fields
:test_id="$question->test_id"
:default_question="$question->question"