Mastering Development

Laravel Validation Array must contain one item where boolean is true

I’m validating question and answers (for test creation). I’d like to ensure that the answers array contains at least one ‘correct’ item. So where answers.*.correct => true.

I currently have the following:

public function rules()
        return [
            'title' => 'required|string|max:255',
            'test_id' => 'required|integer|exists:tests,id',
            'content' => 'required',
            'answers' => 'required|array',
            'answers.*.text' => 'required|string|max:255',
            'answers.*.correct' => 'required|boolean'

At the moment i can miss out adding a correct answer causing an impossible question.

I’ve checked the documentation and can’t see anything that stands out.

Any help would be appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *