Cactiveform, Process String That Is Being Outputed In Updateview

Hi, I have a news-section on my site and when I go to the updateview the characters displayed in title and textarea fields are in the wrong format, so I want to process the characters with:


mb_convert_encoding($model->title,"UTF-8", "Windows-1252")

So the chars is displayed correctly, is this possible?

I solved it in the textfield with this code:




<?php echo $form->textField($model, 'title', array('size'=>60,'maxlength'=>150, 'value'=>mb_convert_encoding($model->title,"UTF-8", "Windows-1252"))); ?>



But that doesn’t work with textArea since textArea dont have the “value” option.

In your model class, modify the returned value from the db to be your converted value. You can set the title field directly, or if you need to preserve it, you can use another property, like so:




    var $convertedTitle;


    public function afterFind() {

        $this->convertedTitle = mb_convert_encoding($this->title,"UTF-8", "Windows-1252");

    }



Then you can use it in your view directly using the convertedTitle attribute (or just title if you decide to overwrite it in afterFind() ).

If you do decide to use convertedTitle, then you also need to override beforeSave() to save the new title to the db. If you decide to just overwrite title then you don’t need this.




public function beforeSave() {

        $this->title = $this->convertedTitle;

        return parent::beforeSave();

    }



Depending on what you’re doing, you may also need to convert back to Windows-1252 in beforeSave(), otherwise it will be stored as a utf-8 value.

Thanks for the reply,

I used convertedTitle like you explained, and it seems to work, but when I try to do the same with content it doesn’t work. Any ideas why?