Skip to main content

yii, some form validation examples

Yii some basic form validations using rules in models
like email, compare password (match password), date, unique, captcha etc

Below is full model code:


<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $user_id
 * @property string $user_name
 * @property string $user_password
 * @property string $user_email
 * @property string $user_gender
 * @property integer $user_age
 * @property string $user_location
 * @property string $user_avatar
 * @property string $user_facebook_id
 * @property integer $user_receive_newsletters_flag
 * @property integer $is_admin
 */
class Users extends CActiveRecord
{
        public $confirm_password, $user_captcha;
 /**
  * Returns the static model of the specified AR class.
  * @return Users the static model class
  */
 public static function model($className=__CLASS__)
 {
  return parent::model($className);
 }

 /**
  * @return string the associated database table name
  */
 public function tableName()
 {
  return 'users';
 }

 /**
  * @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('user_name, user_password, user_email, user_gender, user_date_of_birth, user_location, user_facebook_id', 'required'),
   array('user_age, user_receive_newsletters_flag, is_admin', 'numerical', 'integerOnly'=>true),
   array('user_name, user_password, user_email, user_location, user_avatar', 'length', 'max'=>255),
   array('user_gender', 'length', 'max'=>7),
   array('user_facebook_id', 'length', 'max'=>100),
   array('user_email', 'email', 'message'=>'Email address doesn\'t seem to be valid'),
   array('user_email', 'unique', 'message'=>'This email is already registered.'),
                    
                    
                        array('user_password, confirm_password', 'required', 'on'=>'insert'),
                        array('user_password, confirm_password', 'length', 'min'=>6, 'max'=>40),
                        array('user_password', 'compare', 'compareAttribute'=>'confirm_password'),
                    
                    
                        array('user_date_of_birth', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-mm-dd'),
                    
                        array('user_captcha', 'captcha', 'on'=>'insert', 'message'=>'The entered captcha is incorrect.'),
                    
                    
   // The following rule is used by search().
   // Please remove those attributes that should not be searched.
   array('user_id, user_name, user_password, user_email, user_gender, user_age, user_location, user_avatar, user_facebook_id, user_receive_newsletters_flag, is_admin', 'safe', 'on'=>'search'),
  );
 }

 /**
  * @return array relational rules.
  */
 public function relations()
 {
  // NOTE: you may need to adjust the relation name and the related
  // class name for the relations automatically generated below.
  return array(
  );
 }

 /**
  * @return array customized attribute labels (name=>label)
  */
 public function attributeLabels()
 {
  return array(
   'user_id' => 'User',
   'user_name' => 'Name',
   'user_password' => 'Password',
   'confirm_password' => 'Confirm Password',
   'user_email' => 'Email',
   'user_gender' => 'Gender',
   'user_age' => 'Age',
   'user_date_of_birth' => 'Date Of Birth',
   'user_location' => 'Location',
   'user_avatar' => 'Avatar',
   'user_facebook_id' => 'Facebook',
   'user_receive_newsletters_flag' => 'Receive Newsletters',
   'is_admin' => 'Is Admin',
  );
 }

 /**
  * Retrieves a list of models based on the current search/filter conditions.
  * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  */
 public function search()
 {
  // Warning: Please modify the following code to remove attributes that
  // should not be searched.

  $criteria=new CDbCriteria;

  $criteria->compare('user_id',$this->user_id);
  $criteria->compare('user_name',$this->user_name,true);
  $criteria->compare('user_password',$this->user_password,true);
  $criteria->compare('user_email',$this->user_email,true);
  $criteria->compare('user_gender',$this->user_gender,true);
  $criteria->compare('user_age',$this->user_age);
  $criteria->compare('user_location',$this->user_location,true);
  $criteria->compare('user_avatar',$this->user_avatar,true);
  $criteria->compare('user_facebook_id',$this->user_facebook_id,true);
  $criteria->compare('user_receive_newsletters_flag',$this->user_receive_newsletters_flag);
  $criteria->compare('is_admin',$this->is_admin);

  return new CActiveDataProvider($this, array(
   'criteria'=>$criteria,
  ));
 }





}

?>






Popular posts from this blog

yii, recursive function to create category sub category hierarchy

Create this function in the category model file: ---------------------------------------------------------------------------------  <?php public function get_category_hr ( $cat_id , $selected_cat_id , $level_string ) { $select_str = '' ; if ( ! $level_string ) { $level_string = '' ; } if ( $cat_arr = $this -> findAll ( 'category_parent_id=' . $cat_id )) { foreach ( $cat_arr as $cat ) { $select_str .= "<option value= { $cat -> category_id } " ; if ( $selected_cat_id == $cat -> category_id ) $select_str .= ' selected' ; $select_str .= "> { $level_string }{ $cat -> category_name } </option>" ; $select_str .= $this -...

yii, Executing a SQL without calling a Model

yii, Executing a SQL without calling a Model <? $connection = Yii :: app () -> db ; $sql = "SELECT id,username FROM users" ; $command = $connection -> createCommand ( $sql ); $dataReader = $command -> query (); $rows = $dataReader -> readAll (); print_r ( $rows ); ?>

mysql, import sql file from command line

Method 1: A quick way to duplicate MySQL databases is to do the following: Step 1 : Dump the database either through PHPMyAdmin’s export form or through mysqldump (as I mentioned above). Note (usually, it’s mysqldump <database-name> -h<hostname> -u<username> -p<password> if you ommit the -h switch it will default to “localhost”). After you enter the command below and press enter, you will be prompted for a password because you did not enter the password after the -p switch. $ mysqldump <database-name> -u<username> -p >> somedatabasetable.sql Step 2:  Once you have the sql file, log into mysql and create a database or “use” an existing database – again, ommiting the -h will default to localhost and you will be prompted for a password if you don’t enter a password after the -p switch. $ mysql -u<username> -p Step 3:  After logging in, you will need to either create a new database and select the database for use w...