do not reset saved in model value CFileValidator

Hey everybody, I have a problem with image update.

I have a model with image field. I show the image field in form

<?php echo CHtml::activeFileField($model, ‘image’); ?>

And I have rules for image field

array(‘image’, ‘file’, ‘on’ => ‘insert’),

array(‘image’, ‘file’, ‘on’ => ‘update’, allowEmpty’=>true),

And I assume if user do not update image, I want to leave saved in AR value. But when I do

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

in $_POST[‘image’] I have empty value b/c of hidden yii field for file i.e.

<input id="ytDbShowplace_image" type="hidden" value="" name="DbShowplace[image]" />

and image is safe attribute, so eventually I have $model->image = null

If I don’t use this rule

array(‘image’, ‘file’, ‘on’ => ‘update’, allowEmpty’=>true),

I’ll have an error

2010/08/17 23:10:38 [warning] [application] Failed to set unsafe attribute "image".

Any sugesstions ) ?

Not sure if there’s a better solution, but you can revert to the original value/image this way (within your active record class):





private $_oldImage;


public function afterFind()

{


   parent::afterFind();


   $this->_oldImage = $this->image;

   

}


public function beforeSave()

{


   if (parent::beforeSave())

   {


      if (!$this->isNewRecord && empty($this->image))

      {

         $this->image = $this->_oldImage;

      }


      return true;


   }


}



Yeah it will work. Or we could make some other solutions. But this is a hack. I wish I don’t use any of this solutions.

For example if we use allowEmpty == true do not set this attribute

What do you think?