1

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
5
  • 1
    What do you mean when you say "the issue is that the validation fails even when the request = false"? Commented Feb 17, 2020 at 20:47
  • Your question needs to be reformulated it is not clear what you are trying to accomplish. Commented Feb 17, 2020 at 21:19
  • @KennyHorna for example responses[0]['answer'] = false which i want to consider as a valid answer and should pass the 'required' rule on validator Commented Feb 17, 2020 at 22:07
  • @Mika so for each answer, you want to accept a boolean or a string, am I right? Commented Feb 17, 2020 at 22:19
  • @KennyHorna yes Commented Feb 19, 2020 at 1:19

1 Answer 1

0

Add the nullable rule to the validation string:

 'responses.*.answer' => 'nullable|required_if:responses.*.answer_id,"",
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.