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?

Akhilesh
  • 1,243
  • 4
  • 16
  • 49

2 Answers2

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.

  • Close enough! I used 'required_without' and it worked. 'required_if' is to check if the other field is present. Thanks for your help. – Akhilesh Sep 09 '20 at 07:03
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);
Akhilesh
  • 1,243
  • 4
  • 16
  • 49