Reputation: 275
I'm new to Laravel. I have a form with a File upload function on it. How can I validate image file? . when i execute my code the images are getting uploaded but URL is not saved to the MySQL database, and it shows validation errors on the form as follows.
The thumbnail must be an image. The large image must be an image.
Here's my input and validation code .
{{ Form::open(array('url' => 'admin/templates/save', 'files' => true, 'method' => 'post')) }}
@if($errors->has())
@foreach($errors->all() as $error)
<div data-alert class="alert-box warning round">
{{$error}}
<a href="#" class="close">×</a>
</div>
@endforeach
@endif
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('title','Title:') }}
{{ Form::text('title',Input::old('title')) }}
</div>
</div>
<div class="row">
<div class="small-7 large-7 column">
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',Input::old('description'),['rows'=>5]) }}
</div>
</div>
<div class="row">
<div class="small-7 large-7 column">
{{ Form::label('detailed_description','Detailed description:') }}
{{ Form::textarea('detailed_description',Input::old('detailed_description'),['rows'=>5]) }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('thumbnail','Choose thumbnail image:') }}
{{ Form::file('thumbnail') }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('large_image','Choose large image:') }}
{{ Form::file('large_image') }}
</div>
</div>
<div class="row">
<div class="small-5 large-5 column">
{{ Form::label('targeturl','Target URL:') }}
{{ Form::text('targeturl',Input::old('Target URL')) }}
</div>
</div>
{{ Form::submit('Save',['class'=>'button tiny radius']) }}
{{ Form::close() }}
Controller code
<?php
class TemplateController extends BaseController
{
public function newTemplate()
{
$this->layout->title = 'New Template';
$this->layout->main = View::make('dash')->nest('content', 'templates.new');
}
public function saveTemplate() {
//TODO - Validation
$destinationPath = '';
$filename = '';
$destinationThumb = '';
$thumbname = '';
$thumb = Input::file('thumbnail');
$destinationThumb = public_path().'/thumb/';
$thumbname = str_random(6) . '_' . $thumb->getClientOriginalName();
$uploadSuccess = $thumb->move($destinationThumb, $thumbname);
$file = Input::file('large_image');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
$rules = [
'title' => 'required',
'description' => 'required',
'detailed_description' => 'required',
'targeturl' => 'required',
'thumbnail' => 'required|image',
'large_image' => 'required|image'
];
$validator = Validator::make(Input::all(), $rules);
if ($validator->passes()) {
$template = new Template();
$template->title = Input::get('title');
$template->description = Input::get('description');
$template->thunbnailURL = $destinationThumb . $thumbname;
$template->detailedDescription = Input::get('detailed_description');
$template->targetURL = Input::get('targeturl');
$template->detailImageURL = $destinationPath . $filename;
//$user->created_at = DB::raw('NOW()');
//$user->updated_at = DB::raw('NOW()');
$template->save();
return Redirect::back()->with('success', 'Template added!');
} else
return Redirect::back()->withErrors($validator)->withInput();
}
}
Please help me.
Upvotes: 1
Views: 1568
Reputation: 3335
Move your upload code inside validation passes
public function saveTemplate() {
//TODO - Validation
$rules = [
'title' => 'required',
'description' => 'required',
'detailed_description' => 'required',
'targeturl' => 'required',
'thumbnail' => 'required|image',
'large_image' => 'required|image'
];
$validator = Validator::make(Input::all(), $rules);
if ($validator->passes()) {
$thumb = Input::file('thumbnail');
$destinationThumb = public_path().'/thumb/';
$thumbname = str_random(6) . '_' . $thumb->getClientOriginalName();
$uploadSuccess = $thumb->move($destinationThumb, $thumbname);
$file = Input::file('large_image');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
$template = new Template();
$template->title = Input::get('title');
$template->description = Input::get('description');
$template->thunbnailURL = $destinationThumb . $thumbname;
$template->detailedDescription = Input::get('detailed_description');
$template->targetURL = Input::get('targeturl');
$template->detailImageURL = $destinationPath . $filename;
//$user->created_at = DB::raw('NOW()');
//$user->updated_at = DB::raw('NOW()');
$template->save();
return Redirect::back()->with('success', 'Template added!');
} else
return Redirect::back()->withErrors($validator)->withInput();
}
Upvotes: 3