【发布时间】:2015-12-02 16:32:45
【问题描述】:
我想验证用户只能在表单中上传 Excel 表格。为此,我在规则中尝试了 mimes:xls,xlsx 但它对我不起作用。
而且每当我编辑我的填写表格数据时,它只显示标题但不显示上传文件名。它总是告诉我没有选择文件。我已经上传了这个截图。
这方面有什么帮助吗? !!!谢谢:)
Controller.php 文件
public function store()
{
$uploadsheet = Request::all();
$rules = array(
'title'=>'required',
'graph_sheet'=>'required|mimes:xls,xlsx'
);
$validator = Validator::make($uploadsheet,$rules);
if ($validator->fails())
{
// $messages = $validator->messages();
return Redirect::to(url('/uploadsheet/create'))->withInput()->withErrors($validator);
} else {
$imageName='';
if(Request::file('graph_sheet')!='') {
$imageName = Request::file('graph_sheet')->getClientOriginalName();
$destinationPath = "upload/sheet/";
Request::file('graph_sheet')->move($destinationPath, $imageName);
}
Uploadsheet::create(['title' => $uploadsheet['title'],'graph_sheet' => $imageName]);
return redirect('uploadsheet');
}
}
--------------- 编辑uploadsheet.blade.php 代码------
@extends('app')
@section('content')
<?php if(count($errors)!='0') { ?>
<div class="alert alert-danger">
<?php foreach($errors->all() as $err) { ?>
<?php echo $err;?><br/>
<?php } ?>
</div>
<?php } ?>
<h1>Update Content</h1>
{!! Form::model($uploadsheet,['method' => 'PATCH','route'=>['uploadsheet.update',$uploadsheet->id],'files'=>true]) !!}
<div class="form-group">
{!! Form::label('Title', 'Title:') !!}
{!! Form::text('title',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('Graph Sheet', 'Graph Sheet:') !!}
{!! Form::file('graph_sheet',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
@stop
【问题讨论】:
-
请与我们分享您的观点代码
-
@Saad:我已经编辑了我的代码。你可以检查一下。
-
我看不到任何明显的问题,但仍然尝试
{!! Form::file('graph_sheet') !!}而不是{!! Form::file('graph_sheet',null,['class'=>'form-control']) !!}
标签: laravel-5 laravel-validation