CUploadedFile::getInstance Empty After Post

Hey Everyone,

New user to Yii. I have a problem in that my File Field is empty after posting the form. If the form has no errors I can process the form correctly and process the file. If one of the validation rules fails it is displayed on the page. The problem is that the File Field is now empty on my form. So when the user corrects their errors and has to submit the form they have to reselect the file for the file input.

How do I make the file field keep the same user input the user originally made?

My Controller


if(isset($_POST['StickerOffer']))

		{

			$model->attributes=$_POST['StickerOffer'];

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




			if($model->validate())

			{



The view:


<?php echo $form->fileField($model, 'image'); ?>

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



The model:


	public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('company_name, contact_name, email, abn, address1, suburb, postcode, state_state_id', 'required'),

			array('abn, state_state_id, status', 'numerical', 'integerOnly'=>true),

			array('company_name, contact_name, address1, address2, suburb', 'length', 'max'=>45),

			array('email', 'length', 'max'=>255),

			array('email', 'email'),

			array('abn', 'numerical'),

			array('abn', 'length', 'min'=>11),

			array('abn', 'length', 'max'=>11),

			array('postcode', 'length', 'max'=>4),

			array('postcode', 'length', 'min'=>4),

			array('postcode', 'numerical'),

			array('ip_address', 'length', 'max'=>20),

			array('business_type, date_sent, date_requested', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('sticker_offer_id, company_name, contact_name, email, abn, address1, address2, suburb, postcode, business_type, state_state_id, status, ip_address, file, date_sent, date_requested', 'safe', 'on'=>'search'),

			array('image', 'file', 'types'=>'jpg, pdf','allowEmpty' => false, 'on'=>'insert'),

			array('image', 'file', 'types'=>'jpg, pdf','allowEmpty' => true, 'on'=>'update'),

		);

	}

$image is declared as a public variable at the top of the model class.

I’ve tried enabling Ajax on posting the form and this doesn’t change anything. Any ideas are appreciated.

Put your image in the first line of you rules as "required".

This is a bug (will see if its been reported) - if any of the form elements are not "required", then whatever has been input into them disappears if there is a form error after the post.