Ritcheli's avatar

Validating multiple image files

Hello, I'm trying to validate multiple file types with Laravel validation, but it's not working and I cannot figure out why, this is my code:

Blade form:

<form method="POST" action="{{ route('nova_Pessoa') }}" enctype="multipart/form-data">
			<div class="upload-img-component">
                        <div class="form-row">
                            <div class="form-group col">
                                <label class="text-nowrap">Fotos</label>
                                <div class="input-group CM @error('upload') is-invalid @enderror" id="foto">
                                    <label for="upload" class="label-foto pl-2 ml-1 pt-2 mt-1 text-muted"></label>
                                    <input type="file" class="form-control input-foto" accept="image/png, image/jpg, image/jpeg" id="upload" name="upload[]" multiple>
                                    <div class="input-group-append">    
                                        <label for="upload" class="btn btn-upl-CM m-1"> 
                                            <i class='bx bxs-cloud-upload btn-upl-icon-CM'></i>
                                            Escolha uma ou mais fotos
                                        </label>
                                    </div>
                                </div>
                                @error('upload')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col">
                                <div class="rounded img-preview p-3"></div>
                            </div>
                        </div>
                    </div>
			<button type="reset" data-dismiss="modal" class="btn CM large cancel-CM ml-1 mr-1 shadow-none">Cancelar</button>
             <button type="submit" class="btn CM large save-CM ml-1 shadow-none"> Salvar </button>
</form>

Controller:

public function nova_Pessoa(Request $request){
			$request->validate([
                'nome'              => ['required', 'string', 'max:60'],
                'telefone'          => ['max:15'],
                'CPF_RG'            => ['max:11'],
                'alcunha'           => ['max:60'],
                'observacao_pessoa' => ['max:65535'],
                'upload.*'          => ['mimes:jpg,jpeg,png']
            ]);
}
															
0 likes
3 replies
MohamedTammam's avatar

but it's not working

What's the current behavior?

I guess you're missing some validations.

$request->validate([
  // ...
  `upload` => ['required', 'array']
  'upload.*'          => ['image', 'mimes:jpg,jpeg,png']
]);
1 like
Ritcheli's avatar

@MohamedTammam No error is displayed if I put a file that's not a image, and I'm checking the current value of the $request->upload, and the file is send correctly. My laravel.log

[2023-07-08 20:42:47] local.DEBUG: array (
  'Who_Call' => 'Pessoa',
  '_token' => 'acQkL6Y1HdUovstQC9MPMo4Xwa7JnMqg4EpXWYwb',
  'nome' => NULL,
  'CPF_RG' => NULL,
  'telefone' => NULL,
  'data_nascimento' => NULL,
  'alcunha' => NULL,
  'observacao_pessoa' => NULL,
  'upload' => 
  array (
    0 => 
    Illuminate\Http\UploadedFile::__set_state(array(
       'test' => false,
       'originalName' => 'sid-mencao-i-requerimento.docx.pdf',
       'mimeType' => 'application/pdf',
       'error' => 0,
       'hashName' => NULL,
    )),
  ),
)  

If I put the 'required' validation the error is displayed, but with the mimes is not working. I have a modal with this same validation but with ajax request, and it's working there.

$request->validate([
  // ...
  `upload` => ['required', 'array']
  'upload.*'          => ['image', 'mimes:jpg,jpeg,png']
]);

Sadly this is not working either.

Ritcheli's avatar
Ritcheli
OP
Best Answer
Level 1

Ok, I have found the problem, the validation it's indeed working, what was wrong was how they were shown in the blade, to show the errors with a array variable I need to put with this syntax:

<div class="input-group CM @error('upload.*') is-invalid @enderror" id="foto">
@error('upload.*')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
@enderror

Working with laravel 10

1 like

Please or to participate in this conversation.