Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mdev11's avatar

Validate different quiz inputs (textarea & radio)

I have multiple quizzes, each quiz consists of multiple questions, each question could be (MCQ, true or false or free text).

questions_types:

id | type
1  | MCQ
2  | True or false
3  | Text

questions:

id | question_en | question_fr | type_id
1  | question 1  | question 1  | 1
2  | question 2  | question 2  | 2
3  | question 3  | question 3  | 3

answers:

id | answer_en | answer_fr  | question_id
1  | answer 1  | répondre 1 | 1
2  | answer 2  | répondre 2 | 1
3  | answer 3  | répondre 3 | 1
4  | true      | vrai       | 2
5  | false     | faux       | 2

Quiz blade:

@foreach($questions as $question)
    <h4>{{ $question->question_en }}</h4>
    @if($question->type_id != 3)
		@foreach($question->answers as $answer)
	        <input type="radio" name="answers[{{ $question->id }}]" value="{{ $answer->answer_en }}" @if(old("answers." . $question->id) == $answer->answer_en) checked="checked" @endif>
        @endforeach
    @else
        <textarea name="answers[{{ $question->id }}]">{{ old("answers." . $question->id) }}</textarea>
    @endif
@endforeach

Output:

<h4>question 1</h4>
<input type="radio" name="answers[1]" value="answer 1">
<input type="radio" name="answers[1]" value="answer 2">
<input type="radio" name="answers[1]" value="answer 3">

<h4>question 2</h4>
<input type="radio" name="answers[2]" value="true">
<input type="radio" name="answers[2]" value="false">

<h4>question 3</h4>
<textarea name="answers[3]"></textarea>

Form request validation:

public function rules()
{
    return [
        'answers' => 'required|array',
        'answers.*' => 'required',
    ];
}

But it only validates the textarea. It passes even if no radio is checked. All radio must be filled. This is dynamic so I can't specify the count or values to validate. How to make sure all radio inputs are checked?

0 likes
0 replies

Please or to participate in this conversation.