2
votes

I am building a form which uploads multiple files ,its working fine but the problem is my validation message ,my validation is checking the mime type of the file .

suppose the uploaded file with name filename

The validation message : The file.0 must be a file of type: pdf, doc, docx, jpeg, jpg, png, bnp.

i want to change it to The filename must be a file of type: pdf, doc, docx, jpeg, jpg, png, bnp.

My form :

{!! Form::open(['route' => ['file.store'], 'method'=> 'POST', 'id' => 'file_form','files' => 'true']) !!}

{!!Form::FILE('file[]', ['id' => 'file','multiple' =>true])!!}

 <button type="submit" name="submit">save</button>
{!! Form::close() !!}

my formRequest validation

foreach ($this->file as $key => $val)       
             {                                                

$rules['file.' . $key] = 'mimes:pdf,doc,docx,jpeg,jpg,png,bnp|extension|max:10240'

             }                                             


return rules;     
4
can you please post the request file codes ?Nandakumar
And what should it be for file.1 I Mean for otherNiklesh Raut
@user2486 i want to replace file.1 with the filename itselfwahdan

4 Answers

3
votes

Hi if u are using the FormRequest to validate.

Thats how i achieved it. Here is an example.

public function messages()
 {
  $messages = [];
  foreach($this->request->get('items') as $key => $val)
    {
      $messages['items.'.$key.'.max'] = 'The field labeled "Book Title '.$key.'" must be less than :max characters.';
    }
 return $messages;
}
2
votes

Use custom validation message

Add this line in custom array in validation.php inside resource/lang/en/ folder

'custom' => [
  .........
  'file.*' => [
    'mimes' => 'The filename must be a file of type: :values.',
  ],

I recommend, rename file to unique_name to not conflict with name

2
votes

I appropriate all of you for your answers,i have found another way that fits to my project. I have created custom validation called custom_mimes to validate file type based on mime type ,and adding custom replacer (:filename) to this validation.

  \Validator::extend('custom_mimes', function ($attribute, $file, $parameters, $validator)
        {

           $validator->addReplacer('custom_mimes',  function ($message, $attribute, $rule, $parameters) use ($file)
            {
              $values=implode(',',$parameters);
              return str_replace([':filename',':values'], [$file->getClientOriginalName(),$values], $message);
            });

            $mime_type =$file->guessExtension();
            return in_array($mime_type,$parameters);

        });  

and in validation.php

'custom_mimes'         => 'The :filename must be a file of type: :values.',

so my key and values will be :

:filename => uploaded filename

:values =>'jpg,png,doc,docx'

0
votes

Inside an Form Request you can simply use it like below:

public function messages()
{
    $messages = [];
    foreach($this->request->get('files') as $key => $val)
    {
        $messages['files.'.$key. ". max"] = 'The file field cannot be greater then 10M';
    }
    return $messages;

}