Home > Net >  How can I receive a specific index of an validator array of array validation in Laravel?
How can I receive a specific index of an validator array of array validation in Laravel?

Time:02-21

I validate an array in laravel controller and receive message by using $errors->first('field_name') or $errors or $errors->has('field_name). Now the problem is, I validate an array named vaccine_certificate and I can not receive the error message. Actually I receive the message when show all erros at once. but I want to show it with it's input area. How can I solve it?

$this->validate($request, [
        'name' => 'required',
        'employee_no' => 'required',
         'vaccine_certificate.*' => 'image|mimes:png,jpg|max:2048|dimensions:max_height=200,max_width=200',
        //'expertise' => 'required',
    ]);

Input field looks like

<div >
                    <label  for="vaccine_certificate"><h5 >Vaccine Certificate <small>(<2mb,png,jpg) </small> </h5></label>
                    <input  type="file" name="vaccine_certificate[]" id="vaccine_certificate" multiple/>
                    @if ($errors->has('vaccine_certificate'))
                    <p id="employee_no_checker_message"  onl oad="changeInputBorderColor('vaccine_certificate')"> {{ $errors->first('vaccine_certificate') }}</p>
                    @endif
                    
                </div>

CodePudding user response:

You can use and see all errors in the error bag of form.

$errors->all()

However, as far as I can see there is no "required" in vaccine_certificate, and your rule is vaccine_certificate.*, that means it should be an array. If you make it vaccine_certificate and required like you did in the name field you can see the error message.

If you need to use array, you can use:

$errors->first('vaccine_certificate.*') 

Since it is not "required" it will not throw an error and you can not see it in the $errors variable.

  • Related