CForm and custom validators, setting values inside

Hi,

i have this piece of code:




	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('userid, email, amount, paywith', 'required'),

			array('paywith, userid', 'numerical', 'integerOnly'=>true),

			array('amount', 'numerical', 'integerOnly'=>false, 'min'=>self::$_Options['min'], 'max'=>self::$_Options['max']),

			array('userid, amount', 'length', 'max'=>10),

			array('email', 'length', 'min'=>5, 'max'=>255),

			array('email', 'email'),

			array('amount', 'usercheck', 'on'=>'user'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, userid, email, amount, paywith, date', 'safe', 'on'=>'search'),

		);

	}


	public function usercheck($attribute,$params)

	{

		$this->userid = Yii::app()->user->getId(); // No hacking

		$email = NULL;

		$isvalidemail = Payment::userHavePaymentEmail($this->paywith, $email);

		if(!$isvalidemail)

			$this->addError('paywith', 'You don\'t have an email account for the requested payment.');

		else

			$this->email = $email;

		var_dump($this->email);

		$money = User::myGetMoney();

		if($this->amount > $money)

		{

			$this->addError('amount', 'You don\'t have <strong>'.$this->amount.'&euro;</strong> in your account.');

		}

	}



in my var_dump after email got checked it shows a valid email, the problem is after that values dont keep on variables. maybe yii protection?

and im getting this error:

Note that id and email is never seted on form POST/VIEW

Thanks

please any one?

keep looking for an anwser…

hi!

in your code




                $email = NULL;

                $isvalidemail = Payment::userHavePaymentEmail($this->paywith, $email);

                if(!$isvalidemail)

                        $this->addError('paywith', 'You don\'t have an email account for the requested payment.');

                else

                        $this->email = $email;



you set $this->email = $email if it’s valid email

and $email = NULL (previously in code)

so $this->email = NULL

what else do you expect?

I guess, userHavePaymentEmail sets email if user has payment email, but if he doesn’t, you forget originally entered email (set it to NULL).

Try to modify your code like this:


                if(!$isvalidemail) {

                        $this->addError('paywith', 'You don\'t have an email account for the requested payment.');

                 } else {

                        if (!empty($email))   

                          $this->email = $email;

                 }



Actually, your peace of code isn’t clear at all, I would prefer this


        public function usercheck($attribute,$params)

        {

                $this->userid = Yii::app()->user->getId(); // No hacking

                

                $isvalidemail = Payment::userHavePaymentEmail($this->paywith, $this->email);

                if(!$isvalidemail)

                        $this->addError('paywith', 'You don\'t have an email account for the requested payment.');


                var_dump($this->email);

                $money = User::myGetMoney();

                if($this->amount > $money)

                {

                        $this->addError('amount', 'You don\'t have <strong>'.$this->amount.'&euro;</strong> in your account.');

                }

        }

Cheers,

Vit