【问题标题】:Adding the file name uploaded to the Laravel validation message添加上传到 Laravel 验证消息的文件名
【发布时间】:2017-12-17 11:43:39
【问题描述】:

我有一组正在上传的文件。在validation.php 语言文件中,我添加了自定义错误消息:

'custom' => [
    'images.*'      => [
            'dimensions' => 'Image ":file" must have a min-height of :min_height pixels and a min-width of :min_width pixels.',
            'image'      => 'You can only upload images to your gallery. The file ":file" was not an image.',
            'mimes'      => 'Your image ":file" was an invalid type. Images can only be of the following types: :mimes',
            'max'        => 'Max file size for each image is :max MB. Your image ":file" is too large.'
        ]
]

由于要上传多个文件,我希望能够在显示验证错误时指定错误消息与哪个文件相关。

@if($errors->has('images.*'))
    @foreach ($errors->get('images.*') as $message)
        <div class="help-block has-error">{{ head($message) }}</div>
    @endforeach
@endif

但是,:file 不会自动填充到消息中,我不知道如何获取它。

我该怎么做呢?我可以使用其他占位符或方法吗?

【问题讨论】:

  • 我认为最好为此编写一个自定义图像验证类。
  • @ako 如果有一种标准的简单方法将文件名注入验证消息,我宁愿先走这条路。

标签: laravel laravel-5 laravel-5.4 laravel-validation


【解决方案1】:

我有同样的问题,我尝试做的是获取数组中文件的名称并将其放入自定义错误消息中,这取决于我的示例中的验证类型,我将根据图像类型(jpeg, png, jpg, SVG, BMP)

在控制器文件中:

//...
public function store(Request $request)
    {
$rules = [        'imagefile' => 'required',
                  'imagefile.*' => 'image|mimes:jpeg,png,jpg,svg,bmp', // working on filse size custom message 
                 ];

       

        // To get name of non image file and display in it in error message 
        if($request->hasfile('imagefile')){
           
           $names = array(); // array for all names of files
          foreach($request->Bank_Logo as $file){
            

            // check file extension
            if($file->getClientOriginalExtension() != 'jpeg' &&
               $file->getClientOriginalExtension() != 'png' && 
               $file->getClientOriginalExtension() != 'jpg' &&
               $file->getClientOriginalExtension() != 'svg' &&
               $file->getClientOriginalExtension() != 'bmp'){

               $name= $file->getClientOriginalName (); // get file name 
               $names[] = $name; // set name of file to array 
              
            }
           
          }
        

          // check names array if it empty or not
          if(!empty($names)){
  
            /* but all names in custom error,message 
             * implode funaction to convert array to string 
             */
            $messages = [
                'imagefile.*image' => 'The ' . implode(" and ",$names) . ' not an image file.',
                'imagefile.*mimes' =>  'The ' . implode(" and ",$names) . ' must be a file of type: jpeg, png, jpg, svg, bmp.'
                  ];
          }         
        }              


               if(!empty($messages)){
                  $result = Validator::make($request->all(), $rules, $messages); // with custom message 
                   //dd($result);
               }
               else{ 
                   $result = Validator::make($request->all(), $rules); // without custom message
                    //dd('empty');
               }
        

        if($result->fails())
        {
            return back()->withErrors($result)->withInput($request->all());   
        }
       

        dd($request->all());

      //...
} 

在 balde 文件中

//...
<form>

label class="form-control-label mt-3">{{ __('imagefileLogo') }}</label>
                            <div class="file-loading">
                                <input id="imagefile" name="imagefile[]" type="file" multiple>
                            </div>

                            @if ($errors->has('imagefile'))
                            <span class="invalid-feedback" style="display: block;" role="alert">
                                <strong>{{ $errors->first('imagefile') }}</strong>
                            </span>
                            @endif

                            @if ($errors->has('imagefile.*')) <!--in case if we have multiple file-->
                            <span class="invalid-feedback" style="display: block;" role="alert">
                                <strong>{{ $errors->first('imagefile.*') }}</strong>
                            </span> 
                            @endif    

<form>  
//...

你可以对文件大小做同样的事情。

另外,我使用[这个插件]来上传文件输入 【本插件】:https://plugins.krajee.com/file-input#top

Laravel 5.8 版

我希望这对您有所帮助。 如果有人有更好的方法请讨论。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-06-30
  • 2017-05-30
  • 2017-01-08
  • 2017-02-12
  • 2023-03-03
  • 2020-11-29
  • 2016-02-25
  • 1970-01-01
相关资源
最近更新 更多