skip to Main Content

My form allows users to add fields to upload a file dynamically.

<input type="file" name="your_file_{{ $i }}" id="your_file_{{ $i }}">

So it generates input names like: your_file_1, your_file_2, …

Now I want to make these fields mandatory through validation rules, but I can’t find a way to do that.
I tried with a wildcard but it doesn’t work.

$rules['your_file_*'] = 'required|mimes:pdf,doc,docx,png,jpg,jpeg|max:10000';

Any idea?

2

Answers


  1. To specify a wildcard rule use dot syntax: .*

    $rules = [
        'your_file_.*' => 'required|mimes:pdf,doc,docx,png,jpg,jpeg|max:10000'
    ];
    
    Login or Signup to reply.
  2. for($i = 1; $i <= count($request->all()) - 1; $i++) {
        $validate_array['var'. $i] = 'required';
    }
    
    $this->validate($request, $validate_array );
    

    -1 is for token csrf in post, total loop is for only dinamic input.

    For loop in Laravel validation

    here for references

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search