Problem with model and image upload

I have a model with the following rules:

public function rules()


{


	return array(


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


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


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


		array('strTitle, strText', 'required'),


		array('booPublish', 'numerical', 'integerOnly'=>true),


	);


}

no I have the problem that the create action works perfectly. But when I want to edit some values of an existing entry (but don't want to upload a new image), I always gets an error which says that "strImage" cannot be blank.

How can I keep the existing image, without error message?

for the strImage rule, set the 'on' option to be 'insert'.

Quote

for the strImage rule, set the 'on' option to be 'insert'.

Ok, I've done this, but when I now edit a record and save it, strImage is empty (and i couldn't upload any image) :frowning:

What I want to do, is to keep any existing image (without error message when editing a record), until the user decides to upload a new image for an existing record.

Did you call $model->strImage=CUploadedFile::getInstance(…)?

Yes, I've created a BaseModel and extended my model from it:

class BaseModel extends CActiveRecord

{

  public function afterSave()

  {

    $strDir = Yii::getPathOfAlias('webroot').DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.strtolower($this->tableName()).DIRECTORY_SEPARATOR;

    if ($this->hasAttribute('strImage'))

    {

  $objFile = CUploadedFile::getInstance($this, 'strImage');

  if ($objFile->getError() == 0)

  {

    $this->strImage = $objFile;

    $this->strImage->saveAs($strDir.$this->strImage);

  }

    }

  }

}

I don't think adding 'on' option would cause the problem you described. You are merely disabling a validation rule. It won't affect whether or not 'strImage' is empty.

Ok, no I got it working without any errors:

  public function beforeValidate()

  {

    if ($this->hasAttribute('strImage'))

    {

  $objFile = CUploadedFile::getInstance($this, 'strImage');

  if ($objFile->getError() == 0 && $objFile->getSize() > 0)

  {

    $this->strImage = $objFile;

    return $this->strImage->saveAs($this->getImageDir().$this->strImage);

  }

    }

    return true;

  }

but I've got stil the problem that in case of editing a record, the attribute for strImage is set empty.

I think there must be a hidden field for holding the value for any previously uploaded image, right?

No, there's no such hidden field to store existing file upload.