Yii Framework Forum: custom validate method - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

custom validate method Rate Topic: -----

#1 User is offline   herbary 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 04-June 10

Posted 31 October 2010 - 12:16 PM

Need custom validate method to check is value not equal zero with this way:
    public function rules()
    {
        return array(
            array('username, password, myvalue', 'required'),
            array('myvalue1, myvalue2', 'nozero'),
        );
    }

   public function nozero($attribute,$params)
    {    
        if(!$param==0)
            $this->addError('password','something wrong.');
    }



I have no idea of code of nozero method to get access to myvalue1, myvalue2.
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 31 October 2010 - 01:23 PM

View Postherbary, on 31 October 2010 - 12:16 PM, said:

Need custom validate method to check is value not equal zero with this way:
    public function rules()
    {
        return array(
            array('username, password, myvalue', 'required'),
            array('myvalue1, myvalue2', 'nozero'),
        );
    }

   public function nozero($attribute,$params)
    {    
        if(!$param==0)
            $this->addError('password','something wrong.');
    }



I have no idea of code of nozero method to get access to myvalue1, myvalue2.


   public function nozero($attribute,$params)
    {    
        if($this->$attribute==0)
            $this->addError('password','something wrong.');
    }

0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 31 October 2010 - 01:26 PM

Note that the custom validator function gets the attribute being validated in the parameter $attribute
so for the addError instead of using 'password' you may use $attribute to set the error message for the attribute being validated...
Find more about me.... btw. Do you know your WAN IP?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users