0

I am trying to conditionally validate an array input in Laravel. I am following the documentation provided here. But the documentation does not provide details on how to do it on an array input. Below is the code that I am trying

$rule = [
            'report.*' => 'max:255',
            'comment.*' => 'exclude_if:report.*,file|max:65535'
        ];
        $validator = Validator::make($array, $rule);

Here the report is a file input and comment is a text area field.

<input type="file" name="report[0]"/>
<input type="file" name="report[1]"/>
<textarea name="comment[0]"></textarea>
<textarea name="comment[1]"></textarea>

The comment field is required if the corresponding file input is empty. How can I achieve this in Laravel 7?

2

2 Answers 2

1

Did you try required_if ?

And i think you must write "report" instead of "file" because your input name is report.

If that doesn't work, maybe you need to make a custom rule.

Sign up to request clarification or add additional context in comments.

1 Comment

Close enough! I used 'required_without' and it worked. 'required_if' is to check if the other field is present. Thanks for your help.
0

I was able to do it using below code.

$rule = [
            'report.*' => 'max:255',
            'comment.*' => 'required_without:report.*|max:65535'
        ];
        $validator = Validator::make($array, $rule);

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.