File Upload field becomes empty


I am quiet confused with This problem, it must be a simple one but i cant get around still.

I have a simple form with an upload field. say for an example,

Name : [text]

Address : [text]

Pro Picture : [upload field]

Submit : [button]

Now the problem is, I have validation rules for each field, lets say all are required.

when i leave the form with an empty value [say Name field is left empty] purposely, it will give me the validation error which is obvious.

but sadly, the other parts of the filled form are kept with the data but only the upload field, [pro picture field] is set to empty. :(

so each time, when a validation error occurs user has to upload the pro pic though he had already did so.

why this is happening ? can anybody explain me ? [only for upload field it happens… ] :(

you can allow that field empty using model rule as below,

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

image is field name,

on=>‘update’ is scenario.

You can also refer my This wiki article .

I think you got it wrong kiran.

What i wanted is not when an update occurs. That i have already made. the issue i am having is… when creating, we may enter all required fields nah… unfortunately say if a person forgets to insert his/her address… but have filled the rest which are…

Name and pro picture…

then an error will br thrown nah as address is kept to be required… now my problem is… what i get after validation errors are shown is…

the filled form with error summary where saying you must enter the address…

when i say filled form… i am saying that… it will come pre loaded the Name… and empty address (because i left it so and submitted the form) and [color="#8B0000"]finally the pro picture field which is not getting pre loaded[/color]. so user will have to fill it again and again when a validation error comes when creating the form. :(

why is that ? this is my problem kiran.

Hope i explained clearly this time. :mellow:

let me know if there is a way to get rid from this…

Try this:

	public function actionUpdate($id)




                $oldFileName    = $model->fileName;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);




                        $file   = CUploadedFile::getInstance($model, 'fileName');

                        if ($file !== null)

                            $model->fileName    = $file->getName();


                            $model->fileName    = $oldFileName;


			if($model->save()) {

                                if (!empty($file))









You can see, you’ve the oldFileName stored earlier and then you check if you’ve a new file(in your case a picture).

Hey, still have an issue… what i wanted to be is… the file uploadin field to be loaded with old uploaded image or what ever. this helps only to play with the validation from users end still the field is going to empty.

Not fixed yet. :(

yes got your question,

you want image upload field required but after validation it gets blank what you don’t need.

I had tried to preserve value in image upload input field but not worked.

Hi to all :D

File fields are different then classic text fields… it’s because a file field does not only have the filename in the field… but a file to be uploaded to… you can think of them as they are connected… so you cannot pre-populate a file field with the file name.

To explain the workflow:

  • a user enters some non-valid data for any other field, chooses the file to be uploaded and submits the form

  • at the submit time the HTTP protocol sends the form data togetherwith the chosen file to the server

  • at that time in your action you can access the file with $_FILE that resides in some temporary server folder

  • now… if the validation fails you (we) are displaying (rendering) again the input form, in other words we are sending to the client browser a new form to be shown with the validation errors

  • so as the users gets this new form even if you could pre-populate the previous chosen file name… you cannot in any way pre-populate the file content to be uploaded

There are few solutions that I can think of

  • use ajax/client validation so to be sure that when the form is submited, all the fields will pass the validation.

  • make the picture upload separate (that is the reason why most software like forums have the avatar/profile picture upload in a separate form)

There is some more advanced solutions but they resemble the above second method… like having the whole form with the file upload together and then when the file is selected to upload that file with ajax even before the form is submited… but that is the most complicated and has some more thinking about it as the user can decide to not submit the form but the picture is already uploaded.

1 Like

many thanks for this. i now understand :)

Thanks mdomba, your comments are always helpful.

Thankx and +1 to mdomba.

had the same problem with AJAX-validation and filefields (validation always says the field is blank) I used this approach in performAjaxValidation($model) :

//echo CActiveForm::validate($model);

     echo CActiveForm::validate($model,array('news_date','order_date','header','intro','alias','body'));  // validate all except "file_field"

Thanks Maurizio Domba… :D :D