Laravel4: Validation on file entry

3.9k views Asked by At

I have in my app a simple form to upload photo.

There are three fields: title - caption - and the button for choosing the file. Right now the validation check if the title is required. The caption is not required and is ok.

But i do not know how can i deal with validation regarding files.

If no file is inserted, i get the laravel error message: Call to a member function move() on a non-object. Instead i would like just a validation error. And how can i required that only images are entered? Right now is possible to upload even word documents..

There are the rules as they are now. In the Photo model:

class Photo extends Eloquent {
protected $guarded = array();

public static $rules = array('title' => 'required');
}

Thank you!

1

There are 1 answers

3
Mostafa Elgaafary On BEST ANSWER

Well 1st of all your $rules should be in Photos Model not in the controller , 2nd to make a file required you do it like so :

$rules = array(
  'file'=>'image',
 );

you can add more rules like max file size like so :

$rules = array(
  'file'=>'image|max:2000',
 );