<?php class Mytable extends CActiveRecord{ public static function model($className=__CLASS__){ return parent::model($className); } public function rules() { return array( array('status', 'numerical', 'integerOnly'=>true), array('username, password, firstname, lastname, contactno', 'length', 'max'=>45), array('gender, newsletter', 'length', 'max'=>1), /** Username validation in yii model **/ array('username', 'match' ,'pattern'=>'/^[A-Za-z0-9_]+$/u', 'message'=> 'Username can contain only alphanumeric characters and hyphens(-).'), /** Set scenario for model. Yii Scenario will help you to change dynamic validation using controller. $model=Mytable::model()->findByPk($id); //(OR) $model = new Mytable(); $model->setScenario('updateuser'); // (OR) $model->scenario ='updateuser'; **/ array('username','unique','on'=>'updateuser'), /** EMAIL VALIDATION **/ //Yii M odel Rules For Email array('emailid', 'length', 'max'=>225), array('emailid', 'email'), /** PASSWORD VALIDATION **/ //Yii Model Rules For Password Confirm array('password', 'compare', 'on'=>"confirmpassword", 'compareAttribute'=>'password'), //Yii Model alphanumeric password validation array('password','passwordalphanumeric','on'=>'changepassword'), /** DATE VALIDATION **/ //Yii Model Rules For Date Format array('dob', 'type', 'type' =>'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'), /** SIMPLE PHONE NUMBER VALIDATION **/ //Yii Model Rules For Entering Mobile Or Phone Number array('stdcode,phoneno,mobileno', 'numerical', 'integerOnly'=>true), //Validation without STD CODE NUMBER array('phoneno,mobileno','my_required'), //(OR) //Validation with STD CODE NUMBER array('phoneno,mobileno,stdcode','my_required'), /** TRIM DATA BEFORE SEND TO DATABASE **/ //Yii Model Rules For Trimming Data array('username', 'filter', 'filter'=>'trim'), /** UNIQUE VALIDATION **/ //Yii Model Rules For Unique data array('username', 'unique'), /** Yii Float Number VALIDATION **/ array('ratio', 'match', 'pattern'=>'/^[0-9]{1,3}(\.[0-9]{0,2})?$/'), /** Value In Condition **/ array('status', 'in', 'range'=>array(1,2,3)), ); } // BeforeValidate function in yii rules public function beforeValidate() { if (!$this->phoneno && !$this->mobileno) { $this->addError('mobileno', 'Enter Mobile Number Or Phone Number'); } return parent::beforeValidate(); } // User defined function //Validation without STD CODE NUMBER public function my_required($attribute_name,$params){ if(empty($this->phoneno) && empty($this->mobileno)){ $this->addError($attribute_name, 'Please enter Telephone number or Mobile number'); } } //Validation with STD CODE NUMBER public function my_required($attribute_name,$params){ if(empty($this->phoneno) && empty($this->mobileno)){ $this->addError('phoneno', 'Please enter Telephone number or Mobile number'); }else if(!empty($this->phoneno) && $this->stdcode==''){ $this->addError('stdcode','Please enter STD number'); } } // Check password with alphanumeric validation public function passwordalphanumeric($attribute_name,$params){ if(!empty($this->password)){ if (preg_match('~^[a-z0-9]*[0-9][a-z0-9]*$~i',$this->password)) { // $subject is alphanumeric and contains at least 1 number } else { // failed $this->addError($attribute_name,'Please enter password with digits'); } } } } ?>
Wednesday, July 18, 2012
Yii Model Rules
by Unknown
This tutorial will help you to understand the yii model rules, user defined functions. In yii rules function I added code (of yiiframework) for unique,email,password comparison, date, phone number, trim etc.. I created user functions for alphanumeric password validation, phone number or mobile number requirements validation.
0 comments:
Post a Comment