【问题标题】:Laravel / Validation does not work with my file input?Laravel / 验证不适用于我的文件输入?
【发布时间】:2014-09-11 10:02:18
【问题描述】:

不知道为什么....验证器要么拒绝我所有的文件,因为它们不是图像,要么让它们全部通过... 这是表格:

<!-- The file upload form -->
<form action="{{ URL::route('photographer-gallery-upload-post’) }}" method="POST" enctype="multipart/form-data">
    <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
    <div class="row fileupload-buttonbar">
        <div class="col-lg-7">
            <!-- The fileinput-button span is used to style the file input field as button -->
            <span class="btn btn-success fileinput-button">
                <i class="glyphicon glyphicon-plus"></i>
                <span>Add files...</span>
                <input type="file" name="file[]" multiple>
            </span>
            @if ($errors->has('file'))
                <div class="alert alert-warning" role="alert">
                    {{ $errors->first('file') }}
                </div>
            @endif 

我的验证:

    public function postPhotographerGalleryUpload() {

    // Validation of the files to upload
    $input      = Input::all();
    $rules      = array( 'file' => 'image|max:15000');
    $validator  = Validator::make($input, $rules);
    if ($validator->fails()) {
        return  Redirect::route('photographer-gallery-upload')
                ->withErrors($validator)
                ->withInput();
    } else {
      echo '<pre>';
      print_r(Input::all());
      echo'</pre>';
      die ();}

知道为什么吗??

【问题讨论】:

    标签: php image file-upload laravel-4 validation


    【解决方案1】:

    http://laravel.com/docs/requests#files

    使用 Laravel 打开和关闭表单,如下所示:

    // 'files' => true is necessary
    {{ Form::open(array('url' => 'foo/bar', 'files' => true)) }} 
    {{ Form::close() }}
    

    在您的控制器中,使用Input::hasFile()Input::file() 方法。

    【讨论】:

    • 为什么它不能与基本的
      一起使用?我一直在许多其他页面中使用它并且验证文本输入没有问题......
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2018-01-10
    • 2018-04-13
    • 2017-12-17
    相关资源
    最近更新 更多