File Upload - "Image cannot be blank" Error Message

I keep on getting this weired error message when uploading images

For some reason it doesn’t recognize the uploaded file…

It tells me: "Image1 cannot be blank"

Even though I set:

  • ‘enableAjaxValidation’=>false,

  • ‘htmlOptions’=>array(‘enctype’=>‘multipart/form-data’),

THX!!

Here is my Model validation code:





	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			// UPLOAD RULES


			array('titel', 'required', 'on'=>'upload'),

			array('titel', 'match', 'pattern'=>'#^[0-9A-Za-z ]*$#', 'on'=>'upload'),

			array('titel','length','max'=>45, 'on'=>'upload'),	// PW only 20 Chars

			array('titel','length','min'=>6, 'on'=>'upload'),	// PW min 6 Chars

			array('description', 'required', 'on'=>'upload'),

			array('description', 'match', 'pattern'=>'#^[0-9A-Za-z\:\-\.\, ]*$#', 'on'=>'upload'),

			array('description','length','min'=>6, 'on'=>'upload'),	// PW min 6 Chars

			array('image1', 'required', 'on'=>'upload'),

			array('image1', 'file', 

				'types'=>'jpg, gif, png', 

				'maxSize'=>1024 * 1024 * 5,

                		'tooLarge'=>'The file was larger than 5MB. Please upload a smaller file.',

				'wrongType'=>'Please upload only images in the format jpg, gif, png',

				'tooMany'=>'You can upload only 1 avatar',

			'on'=>'upload'),

		);

	}



Here is the view file:





<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'image-form',

	'enableAjaxValidation'=>false,

	'htmlOptions'=>array('enctype'=>'multipart/form-data'),

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>




	<div class="row">

		<?php echo $form->labelEx($model,'titel'); ?>

		<?php echo $form->textField($model,'titel'); ?>

		<?php echo $form->error($model,'titel'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'description'); ?>

		<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>

		<?php echo $form->error($model,'description'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'year'); ?>

		<?php echo $form->dropDownList($model,'year',array($model->getYearOptions(),)); ?>

		<?php echo $form->error($model,'year'); ?>

	</div>




	<div class="row">

		<?php echo $form->labelEx($model,'image1'); ?>

		<?php echo CHtml::activeFileField($model, 'image1'); ?><br>

		<?php echo $form->error($model,'image1'); ?>

	</div>











	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>






Never Mind…

I forgot to create a CUploadedFile in my Controller…


$model->image1=CUploadedFile::getInstance($model,'image1');

What a dumb mistake!