Validate an Uploaded FileFormat is Excelsheet Only in Laravel 5

I want to validate that user can upload only excel sheet in form. For that I have try mimes:xls,xlsx in rules but it didn't work for me.

And also whenever I have edit my fill up form data its show me only title but not shown upload file name. it always show me No file selected. I have upload screenshot of this.

Any help in this? !!! Thanks :)

Controller.php File

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');
        }

   }

--------------- edituploadsheet.blade.php code ------------------

@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

Answers


To display your Input field properly

<div class="form-group">
{!! Form::labelControl('image','upload_Img',['class'=>'control-label col-md-3'])  !!}

<div class="col-md-4 fileinput fileinput-{{ isset($edit)  ? 'exists': 'new' }}" data-provides="fileinput">
    <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
        {!! HTML::image(route('image','no-image.png')) !!}
    </div>
    <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;">
        @if(isset($edit) && !empty($edit))
        {!! HTML::image($fileinput_preview) !!}
        @endif
    </div>
    <div>
        <span class="btn default btn-file">
            <span class="fileinput-new">
                {{ trans('admin/admin.select_image') }}</span>
            <span class="fileinput-exists">
                {{ trans('admin/admin.change') }}</span>
            {!! Form::file('image', null) !!}
        </span>
        <a href="javascript:;" id class="btn default fileinput-exists" data-dismiss="fileinput">
            {{ trans('admin/admin.remove') }}</a>
    </div>
</div>


Need Your Help

How do I create custom rails namespace routes

ruby-on-rails-3 namespaces routes

I'm starting to use routes with namespaces for the first time. I understand the concept of what the following does.

Make service indepented from activity (Android)

android service android-activity

I have an activity with two buttons, start and stop. If the user press the start button a service is created using Context.startService. And the stop button calls Context.stopService.