Model rules: filter with chtml::encode

Hello, I have a question.

How can I use chtml::encode in rules model?

I have tried:




       /**

         * Declares the validation rules.

         */

        public function rules()

        {

                return array(

                        array('name, email, subject, body', 'required'),

                        array('email', 'email'),

                        array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

                        array('name, email, subject, body', 'filter', 'filter' => array($this,'sanitaze'))

                );

        }


    public function sanitaze($value = NULL)

    {

        return Chtml::encode($value);

    }



but i get an error.

Custom validator methods must have a signature like this:




public function sanitize($attribute, $params)



but it can be simpler in your case:




'filter' => array('CHtml', 'encode'),



EDIT: Sorry, the first part of course doesn’t apply to filter methods.

As for the error you got: it’s probably because you try to call Chtml::encode() instead of CHtml::encode(). Although class names aren’t case sensitive in PHP, class autoloaders can be, depending on the OS.

thanks a lot guys, I will try to deepen the custom validator for future project

Maybe this is just my opinion, but I don’t think it’s best practice to encode before saving to your DB.

You should instead save data as-is, and encode before rendering / display.