Laravel 5.4 FormRequest消息不起作用

时间:2018-08-08 10:30:14

标签: validation laravel-5

谁能帮忙为什么“ if”部分中数组中的多个消息不起作用?如果无效,则“ else”部分工作仅适用于单个上传错误消息。

public function rules()
{
    return [
      'fileupload' => 'bail|required|max:1024|mimes:gif,jpg,jpeg,png',
    ];
}

public function messages()
{
  $field      = 'fileupload'; // Input fieldname


  $uploads    = $this->file($field);

  $messages = [];
  $index    = 0;

  if (is_array($uploads)) {

    foreach ($uploads as $file) {
      $messages[$field . '.' . $index . '.mimes']    = $file->getClientOriginalName() . ' must be a file of type: :values.'; 
      $messages[$field . '.' . $index . '.max']      = $file->getClientOriginalName() . ' must not be greater than: :max kilobytes.'; 
      $messages[$field . '.' . $index . '.uploaded'] = 'Failed to upload ' . $file->getClientOriginalName() . ' check file type and size'; 
      $index++;
    }

  } else {
    $messages = [
      $field . '.mimes'    => $uploads->getClientOriginalName() . ' must be a file of type: :values.', 
      $field . '.max'      => $uploads->getClientOriginalName() . ' must not be greater than: :max kilobytes.', 
      $field . '.uploaded' => 'Failed to upload ' . $uploads->getClientOriginalName() . ' check file type and size', 
    ];
  }

  // dd($uploads, $messages);

  return $messages;
}

谢谢!

0 个答案:

没有答案