MultiFile Upload Validation In Laravel (v5.2) / Pt.1

- this is using the same workflow as Here

Update
a better validation error retrieval & display is added check Pt.2

  • validations\Post\StoreValidation
// ...

class StoreValidation
{

  // ...

  protected function validate($request)
  {
    $rules = [
      // ...
      'photo.*' => '...',
    ];

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

    if ($validator->fails()) {

      foreach ($validator->errors()->getMessages() as $k => $v) {

        // we search for any error that has "word.number"
        $last= [];
        if (preg_match("/\w+\.\d+/", $k)) {
            // save into new array
            $last[] = $v;
        }
      }

      // flatten the array into one level
      $res = array_flatten($last);
      $validator->errors()->add('photo_err', $res);

      $this->throwValidationException($request, $validator);
    }
  }
}
  • presenter
public function getPhotoError($array)
{
  $array['photo_err'] = call_user_func_array('array_merge', $array['photo_err']);
  return $array['photo_err'];
}
  • view
@inject('pres','presenter-Class-name')

@if ($errors->getMessages())
  @foreach ($pres->getPhotoError($errors->getMessages()) as $one)
    <li>{{ $one }}</li>
  @endforeach
@endif

#Extended

** what if you have more than one array in the same form ?, like photos and videos for example

- same as b4 but we add the new item and change the regex to be more explicit

  • validations\Post\StoreValidation
class StoreValidation
{

  // ...

  protected function validate($request)
  {
    $rules = [
      // ...
      'photo.*' => '...',
      'video.*' => '...'
    ];

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

    if ($validator->fails()) {

      // photos array errors
      $photos = [];
      foreach ($validator->errors()->getMessages() as $k => $v) {
        if (preg_match("/photo\.\d+/", $k)) {
          $photos[] = $v;
        }
      }
      $res = array_flatten($photos);
      $validator->errors()->add('photo_err', $res);

      // videos array errors
      $videos = [];
      foreach ($validator->errors()->getMessages() as $k => $v) {
        if (preg_match("/video\.\d+/", $k)) {
          $videos[] = $v;
        }
      }
      $res = array_flatten($videos);
      $validator->errors()->add('video_err', $res);

      $this->throwValidationException($request, $validator);
    }
  }
}
  • presenter
public function getPhotoError($array)
{
  $array['photo_err'] = call_user_func_array('array_merge', $array['photo_err']);
  return $array['photo_err'];
}

public function getVideoError($array)
{
  $array['video_err'] = call_user_func_array('array_merge', $array['video_err']);
  return $array['video_err'];
}
  • view
@inject('pres','presenter-Class-name')

@if ($errors->getMessages())
  <!-- photo error -->
  @if (count($pres->getPhotoError($errors->getMessages())))
    @foreach ($pres->getPhotoError($errors->getMessages()) as $one)
      <li>{{ $one }}</li>
    @endforeach
  @endif

  <!-- video error -->
  @if (count($pres->getVideoError($errors->getMessages())))
    @foreach ($pres->getVideoError($errors->getMessages()) as $one)
      <li>{{ $one }}</li>
    @endforeach
  @endif
@endif
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