MultiFile Upload Validation In Laravel (v5.3)

- this is using the same workflow as Here

Update
code was working fine until “5.3.15” but updates after that build has screwed everything up, so post updated to tackle that.

Update 2
if you prefer to use the native form-request instead Check

  • view
{{ Form::open(['route' => 'store.files', 'method' => 'POST', 'files'=>true]) }}
  {{ Form::file('image[]', ['multiple']) }}
  {{ Form::submit('submit') }}
{{ Form::close() }}

@foreach ($errors->get('image') as $one)
  <li>{!! $one !!}</li>
@endforeach
  • validations\Post\StoreValidation
// ...

class StoreValidation
{

  // ...

  protected $error_container = [];


  protected function validate($request)
  {
    $rules = [
      // ...
      'image' => 'required'
    ];

    $validator = Validator::make($request->all(), $rules);

    $validator->after(function ($validator) use ($request) {

      // 1- save the input name
      $input_name = 'image';

      // 2- check if we have items in the array
      if ($request->hasFile($input_name)) {

        // 3- save the part of the validation message we want to replace
        // which usually starts with 'The :attribute'
        // but we use regex instead to make it flexible across app locales 
        $regex = '/^.*?'.$input_name.'/';;

        // 4- loop on each file
        foreach ($request->file($input_name) as $one) {

          // 5- validate each file
          $v = Validator::make(
            [$input_name => $one], 
            [$input_name => 'image|max:2048|etc...']
          );

          if ($v->fails()) {
            // the file name with some styling to make it pop-out
            $new_name = "<strong>{$one->getClientOriginalName()}</strong>";

            // 6- replace the validation original msg with the file name
            $str = preg_replace($regex, $new_name, $v->errors()->getMessages()[$input_name][0]);

            // 7- add the new message to the error container
            $this->error_container[$input_name][] = $str;
          }
        }

        // 8- only add items to the messageBag if we 
        // actually have any validation errors
        if (!empty($this->error_container)) {
          $validator->errors()->add($input_name, $this->error_container[$input_name]);
        }
      }
    });


    // fire an exception and redirect on fail
    if ($validator->fails()) {
      throw new ValidationException(
        $validator,
        $this->buildFailedValidationResponse(
          $request,
          array_replace_recursive(
            $this->formatValidationErrors($validator)
            $this->error_container
          )
        )
      );
    }
  }
}

. same call as anything else https://laravel.com/docs/5.3/validation#working-with-error-messages except that it actually work

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s