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,
));
}





}

?>

Leave a comment

Your email address will not be published. Required fields are marked *