[SOLVED] display modified field data

Hi, I need to display field data in a different way in the form. For instance, value ‘ffffff’ will be displayed as ‘255,255,255’ (so from hex to dec). On the contrary, when form validates value as ‘255,255,255’ will be transformed in ‘ffffff’

Now my problem is: how to do that?

its not a problem i think.

all you have to do in your validation method

converting hex to rgb format then displaying validation result.

only trick may be validating rgbs. but its also not a big deal

if your validation rule is to only check whether its in rgb format

or not.

This thread has been discussed and resolved on #yii at freenode.

First of all I would like to thank Pestaa an his patience for help me in #yii channel. I’m very new in Yii and in OOP in general, so you know it can be frustating :D

Now the problem and how I’ve (Pestaa, better) solved. The guy who designed “backgrounds” table decided to store background color in RGB decimal format, comma separated, like “255,204,204”. Very smart guy, uh? :)

It was impossible to ask to my customer to change background value manually, using a input type text, so I decided to use a very nice JQuery plugin, called ColorPicker: http://www.eyecon.ro/colorpicker/

Better way to use that plugin is to use rgb in hex format, like "ffcccc".

I had first to convert decimal comma separated values in hex format, then change hex value using ColorPicker and then store in table in decimal comma separated again. I’ve tried to put logic in view but it wasn’t a good solution.

An important thing that I’ve learned is “getter function”. A getter function is a model function like getFoo() that you can recall in your view using $model->foo. Another thing I’ve learned is that a getter function doesn’t need parameter (as I was trying to do :P). So public function getFoo(){} in model and $model->foo (and not $model->foo() in the view).

I needed also a function to convert from hex to dec and from dec to hex.

So:




        /**

        * @return converted hex. Example: from 255,204,204 to ffcccc

        */

        public function colorHex($colors){

        $dec = explode(',',$colors);

        $r = dechex($dec[0]);

        $g = dechex($dec[1]);

        $b = dechex($dec[2]);

        $rgb = $r.$g.$b;

        return $rgb;

        }


        /**

        * @return converted dec. Example: from ffcccc to 255,204,204

        */

        public function colorDec($colors){

                $dec = hexdec(substr($colors,0,2)).",".hexdec(substr($colors,2,2)).",".hexdec(substr($colors,4,2));

                return $dec;

        }


       /**

       * now, the getter functions

       * background_color is the field where bg values are stored

       */

       public function getHexBackground(){

                return $this->colorHex($this->background_color);

        }


       // this getter function return decimal values using colorDec one

       public function getDecBackground(){

                return $this->colorDec($this->background_color);

        }


       /**

       * to store corrected value I use decBackground function in beforeValidate method

       */

       public function beforeValidate(){

                $this->background_color = $this->decBackground;

        }



Please correct me if I’ve done something wrong, but it seems it works like a charms :)))

[EDIT] Of course in the view I use $model->hexBackground to display correct hex values