Laravel形成mime类型验证

时间:2016-04-22 14:32:43

标签: php validation laravel

表格是文件上传者。如果mime类型的验证失败,Laravel不会给我错误。如果输入失败,则会在我的上传页面上错误地给出错误。看起来验证是正确的,但如果mime类型错误,它只会给我回错,因为如果文件错误,它不会上传文件。

路线

Route::post('/newfile', function (Request $request) {

$validator = Validator::make($request->all(), [
    'userFile' => 'required|mimes:zip',
]);

if ($validator->fails()) {
    return redirect('/upload')
        ->withErrors($validator);
} else {

视图

   @include('errors.errors')

<form action="{{ url('newfile') }}" method="POST" id="uploadForm" class="form-horizontal" enctype="multipart/form-data">
    {!! csrf_field() !!}

    <div class="input-group">
                <span class="input-group-btn">
                    <span class="btn btn-primary btn-file">
                        Browse&hellip; <input name="userFile" id="userFile" type="file" />
                    </span>
                </span>
        <input type="text" class="form-control" readonly>
    </div>

    <div>
        <button type="submit" id="btnSubmit" value="Submit" class="btn btn-success">Upload</button>

        <div class="progress">

        </div>
    </div>
</form>

错误

    @if (count($errors) > 0)
        <!-- Form Error List -->
<div class="alert alert-danger">
    <strong>Whoops! Something went wrong!</strong>

    <br><br>

    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

1 个答案:

答案 0 :(得分:0)

已修复,我上传的javascript代码阻止了验证程序的错误。