Social Icons

Pages

Saturday, May 11, 2013

Yii Model On Insert, Update And Change Password And Alphanumeric password



In Model.php Add this rules
public function rules()
{
        
   return array(
 array('username, password,confirmpassword,firstname,
               lastname, email,address, companyname, usertype,
               isactive',       'required',
               'on'=>'insert'),
        array('username, firstname,lastname, email,address,
        companyname, usertype,isactive', 'required',
              'on'=>'updateuser'),
        array('password,confirmpassword','required',
           'on'=>'changepassword'),
            array('password', 'compare', 'compareAttribute' =>
              'confirmpassword','on'=>'insert,changepassword'),
             
           array('password','passwordalphanumeric',
             'on'=>'changepassword'),   
            
            array('username,password,firstname,lastname,
            email,address,companyname,phoneno,mobileno',
            'filter', 'filter'=>'trim'),
}
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,'Enter password with digits');
       }       
    }
}


Friday, May 10, 2013

Rules and validation in Yii Model

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.


<?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');
     } 
}
}
}
?>

Yii for beginners

Wednesday, May 8, 2013

Adding Criteria To Validation Rules

Sometimes you want your validation rules to be just a little bit more than the standard options available to you like ‘numeric’, ‘length’ or ‘safe’.
array('username', 'unique', 'className' => 'User');

But sometimes you want unique to be more intelligent with it’s unique validation…
array('username', 'unique', 'className' => 'User', 'criteria' => array(                 'condition' => 'active = 1',         )
)
The test above will only match when username is not unique AND active = 1. In the case of unique, criteria you define is appended to the existing check which would always take place.