【问题标题】:Laravel 5 FormRequest custom message for array file fieldLaravel 5 FormRequest 数组文件字段的自定义消息
【发布时间】:2015-07-02 19:49:49
【问题描述】:

我们如何为表单数组字段设置自定义消息?让我通过向您展示我的一些代码来进行解释。

Form Field:
{!! Form::file('doc[]', array('multiple'=>true)) !!}

FormRequest:
public function validator($factory)
    {
        $v = $factory->make($this->all(), $this->rules());
        $v->each('doc', ['required','mimes:doc,docx,jpg,jpeg,png|max:2048']);
        return $v;
    }

默认显示:

  • doc.1 必须是以下类型的文件:doc、docx、jpg、jpeg、png。
  • doc.2 必须是以下类型的文件:doc、docx、jpg、jpeg、png。

但我想表演

  • {filename} 必须是以下类型的文件:doc、docx、jpg、jpeg、png。

【问题讨论】:

    标签: validation laravel-5


    【解决方案1】:
     $file = $request->file('user_detail');
        $credentials =[
            'file'      => $file,
            'extension' => strtolower($file->getClientOriginalExtension()),
        ];
    
        $rules = [
            'file'          => 'required',
            'extension'      => 'required|in:xlsx',
        ];
        $msg = [
            'file.required' => "file is required.",
            'extension.in' => "only accept .xlsx file.",
    
        ];
    
        $validator = Validator::make($credentials,$rules,$msg);
        if($validator->fails())
        {
            return $validator->messages()->all();
        }
    

    【讨论】:

      【解决方案2】:

      我找到了为表单数组字段设置自定义消息的方法。

      public function validator($factory)
          {
              $messages = [
                  'doc.0.required' => "You must provide at least one document.",
              ];
      
              if(Request::hasFile('doc')) {
                  $docs = Request::file('doc');
                  foreach ($docs as $key => $val) {
                      $messages['doc.' . $key . '.mimes'] = 'The document ' . $val->getClientOriginalName() . ' must be a file of type: :values.';
                      $messages['doc.' . $key . '.max'] = 'The document ' . $val->getClientOriginalName() . ' may not be greater than :max kilobytes.';
                  }
              }
      
              $v = $factory->make($this->all(), $this->rules(),$messages);
              $v->each('doc', ['required','mimes:pdf,doc,docx,jpg,jpeg,png|max:2048']);
              return $v;
          }
      

      【讨论】:

        【解决方案3】:

        这绝对是可能的。一种方法是覆盖你的validation.php中的语言文件或遵循http://laravel.com/docs/5.0/validation#custom-error-messages中定义的任何其他方法

        在validation.php中你会发现

        "mimes"                => "The :attribute must be a file of type: :values.",
        

        改成

        "mimes"                => "The documents must be a file of type: :values.",
        

        如果您想在消息中包含文件名,请执行以下操作

        $messages = [
          'mimes' => "The $fileName must be a file of type: :values.",
        ];    
        $v = $factory->make($input, $rules, $messages);
        

        【讨论】:

        • 我已经尝试过这些东西但没有成功,你能告诉我一些我用来更改验证消息的代码
        • 为您编辑了答案。
        • 如果我想显示文件名而不是文档怎么办?
        • 我希望这个编辑现在足以接受答案:)
        • 是的,你说得对。但是这个 $fileName 是从哪里来的?
        猜你喜欢
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 2015-10-04
        • 2020-10-08
        • 2019-04-23
        • 2015-03-04
        • 2017-11-19
        • 2017-08-17
        相关资源
        最近更新 更多