Laravel / Validation不适用于我的文件输入?

时间:2014-07-20 23:35:37

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

无法弄明白为什么......验证者要么拒绝我的所有文件,要么拒绝让他们全部通过... 这是表格:

<!-- 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 ();}

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

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()方法。