How to add a callback function

Hi @ all !

I tried many things without success, like this (on the model):


	public function rules()

	{

		return array(

			array('name',array('filter'=>$this->myfilter())),

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

			array('name', 'required'),

			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),

		);

	}

or


	public function rules()

	{

		return array(

			array('name','filter',$this->myfilter($_POST['posts']['name'])),

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

			array('name', 'required'),

			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),

		);

	}

My "my_filter" function is located on the current model ($this) and takes a string as input and return another string as output.

I got error messages like :


Missing argument 1 for mymodel::myfilter(), called in...

So can you please give me the correct syntax ?

Why don’t you use beforeValidate() for that? Just add that method and filter your attributes the way you want.

You can use $this->name inside the function myfilter() instead of send it as the parameter.

Can you please give me the syntax?

because i dont find any example in the docs.

is it something like ? :


        public function rules()

        {

                return array(

                        array('name','beforeValidate'),

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

                        array('name', 'required'),

                        array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),

                );

        }


	

	public function beforeValidate()

	{

		$_POST['users']['name'] = $this->myfilter($_POST['users']['name']);

	}




You can for example do something like this:


public function beforeValidate()

{

  foreach ($this->attributes as $name => $val)

     $this->$name = trim($val);


  return true; // Required, else validation will not be performed

}

Ok thanks !

Check the LoginForm model in the blog tutorial for example(authenticate rule):




class LoginForm extends CFormModel

{

...

        public function rules()

        {

                return array(

                        ...

                        array('password', 'authenticate'),

                );

        }


        public function authenticate($attribute,$params)

        {

                if(!$this->hasErrors())  // we only want to authenticate when no input errors

                {

                        $identity=new UserIdentity($this->username,$this->password);

                        $identity->authenticate();

                        switch($identity->errorCode)

                        {

                                case UserIdentity::ERROR_NONE:

                                        $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

                                        Yii::app()->user->login($identity,$duration);

                                        break;

                                case UserIdentity::ERROR_USERNAME_INVALID:

                                        $this->addError('username','Username is incorrect.');

                                        break;

                                default:

                                        $this->addError('password','Password is incorrect.');

                                        break;

                        }

                }

        }

...

}