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





          //...the uploaded file is validated, take your desicion <img src='' 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


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


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

$model = new AdminEditCatgory();



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

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



    &#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);









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 ;)