Form - Image Just If Choose

How can I use rules (FormModel) just if the user choose a file

edit: I mean in controller, how can I check if use choose a file in controller

In FormModel

public function rules()

{


    return array(


        array('file_id', 'file', 'types'=>'pdf,jpg, gif, png'),


    );


}

In controller

$model->attributes=$_POST[‘Item’];

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


        if($model->validate())


        {


          //...the uploaded file is validated, take your desicion <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />


        }

if you want to do that automatically just add

array(‘file_id’, ‘file’,‘allowEmpty’=>true, ‘types’=>‘pdf,jpg, gif, png’)

if I don’t choose image it show “Trying to get property of non-object”

ok, after of

$model->image=CUploadedFile::getInstance($model,‘file_id’);

you can check if the file is uploaded

if ($model->image === null) {

//no file submitted

}

OR by pured php code

if (!isset($_FILES[‘file_id’])) {

//no file submitted

}

Ok thanks

I’ve done this

[PHP]

$catgory = Catgory::model()->findByPk($id);

$model = new AdminEditCatgory();

if(isset($_POST[‘AdminEditCatgory’]))

{

&#036;model-&gt;attributes = &#036;_POST['AdminEditCatgory'];


&#036;model-&gt;image = CUploadedFile::getInstance(&#036;model, 'image');





if(&#036;model-&gt;validate())


{


    &#036;catgory-&gt;name = &#036;model-&gt;name;





    if (&#036;model-&gt;image === null) {


        &#036;catgory-&gt;image_type = Helper::getType(&#036;model-&gt;image-&gt;name);


        &#036;model-&gt;image-&gt;saveAs(


            DIR.'/static/images/catgory/'.&#036;catgory-&gt;id.'.'.Helper::getType(&#036;model-&gt;image-&gt;name)


        );


    }





    &#036;catgory-&gt;save();


}

}

[/PHP]

but nothing hasn’t change… the same problem “Trying to get property of non-object” when I don’t choose a picture but if I choose then their is no problem.

Thanks for help ;)

$model->image !== null instead of $model->image === null

Thank you very much ;)