Friday, February 21, 2014

Yii - model custom validation eample


   public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('projid, projdate', 'required'),
			array('projid', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, projid, projdate', 'safe', 'on'=>'search'),
                        array('projdate', 'datevalid'),
                        
		);
	}
        
        
        public function datevalid($attribute)
        {
           $projid = $this->projid;
           $projdate    = $this->projdate;
           $conn = Yii::app()->db;
           if($projid!='')
           {
                $cmd  = $conn->createCommand("select startdate from project where id={$projid}");
                $date = $cmd->queryColumn();
                if(strtotime($projdate)<=strtotime($date[0]))
                {
                  echo $this->addError($attribute,"Close Date you are Entered Should not less than or Equal to project start date");
                }
           }
            
            
        }

No comments:

Post a Comment