I am trying to validate an image file uploaded by users
I have the model:
class Media extends CActiveRecord
{
public $file;
function rules(){
return array(
....
array('file', 'file', 'types' => 'gif, jpg, jpeg, png', 'allowEmpty' => false),
....
}
public function beforeValidate(){
$this->file = CUploadedFile::getInstance( $this, 'file');
if($this->file!=null){
//do stuff
}
return parent::beforeValidate();
}
}
the view file has this:
<label class="control-label" for="Media_file">Upload new picture:</label>
<div class="controls">
<?php echo $form->fileField( $media, 'file' ); ?>
<?php echo $form->error( $media, 'file' ); ?>
</div>
but I can not pass the validation, it always output "file can not be blank", even through I do upload a real image, does body has an idea what is going wrong here? thanks for your help.