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, return to previous url after login or logout

If you want to return to your previous url after login or logout try this : <?php $this -> redirect (Yii :: app () -> request -> urlReferrer ); ?> To set the return url to be the url that was before the login page or registeration page was called you can put following code in views/layouts/main.php file : <?php //this checks id the controller action is not 'login' then it keeps the current url in returnUrl if (CController :: getAction () -> id != 'login' ) { Yii :: app () -> user -> setReturnUrl (Yii :: app () -> request -> getUrl ()); } ?>

Yii2: Using csrf token

Yii2: Using csrf token First, if you do not understand what is the CSRF token? and why should we use it, please refer to the following link : https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF) One of the new features of Yii2 is CSRF validation enabled by default. If you use ajax or basic form as follows : <form action='#' method='POST'> ........... </form> You will get an error exception : Bad Request (#400): Unable to verify your data submission That is because you do not submit csrf token. The easiest way if you dont care about csrf just disable it in main config : 'components' => [ 'request' => [ .... 'enableCsrfValidation'=>false, ], ..... ], Or in Controller : public function beforeAction($action) { $this->enableCsrfValidation = false; return parent::beforeAction($action); } So how to use Csrf Validation for your strong security website: * Wi