Mika
Mika

Reputation: 41

Laravel validator fails if response is false

I have a laravel validator on my request that is set to required, the issue is that the validation fails when the responses[0]['answer'] = false. i have a checklist in the frontend that the user submits either true or false, so i want to consider false as a actual response and should pass the validation, how can i allow that to pass validation when the responses[0]['answer'] = false

{
    "id": 50,
    "type": "forum",
    "responses": [
        {
            "question_id": 89,
            "answer": false
        },
        {
            "question_id": 90,
            "answer": true
        },
        {
            "question_id": 91,
            "answer": "this is a input type answer"
        }
    ]
}

and this is how the validator looks

$request->validate([
 'id' => 'required',
 'responses.*.question_id' => 'required',
 'responses.*.answer_id' => 'required_if:responses.*.answer,""',
 'responses.*.answer' => 'required_if:responses.*.answer_id,"",
]); 

i validate that

responses['*']['answer'] 

is required but i want to allow it even if

responses['*']['answer'] = false

Upvotes: 1

Views: 305

Answers (1)

Erubiel
Erubiel

Reputation: 2972

Add the nullable rule to the validation string:

 'responses.*.answer' => 'nullable|required_if:responses.*.answer_id,"",

Upvotes: 0

Related Questions