Yii framework has awesome input validation mechanism, one of the many validation rules is “compare” rule. You can utilize it for creating confirmation fields to verify user input. Its a good practice to get password input twice while creating signup and change password forms. class User extends CActiveRecord { public $repeat_password; public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'users'; } public function rules() { return array( array('username', 'required'), array('password, repeat_password', 'required', 'on' => 'create'), array('password, repeat_password', 'length', 'min' => 6, 'max' => 30, 'on' => array('create', 'update')), array('password', 'compare', 'compareAttribute' => 'repeat_password', 'on' => array('create', 'update')), array('password, repeat_password', 'length', 'min' => 8), array('username', 'length', 'max' => 60), array('username', 'unique'), array('id, username', 'safe', 'on' => 'search'), ); } public function attributeLabels() { return array( 'id' => 'ID', 'username' => 'Username', 'password' => 'Password', ); } public function search() { $criteria = new CDbCriteria; $criteria->compare('id', $this->id, true); $criteria->compare('username', $this->username, true); return new CActiveDataProvider($this, array( 'criteria' => $criteria, )); } public function encrypt($parameter) { return md5($parameter); } public function beforeSave() { if (!empty($this->password) && strlen($this->password)) { $this->password = $this->encrypt($this->password); } else { if (empty($this->password)) $this->password = $this->findByPk($this->id)->password; } return parent::beforeSave(); } }
Yii password, repeat password fields
Yii framework has awesome input validation mechanism, one of the many validation rules is “compare” rule. You can utilize it for creating confirmati
Related posts
Yii
Remove index.php from URL in YII
Learn how to configure Yii's UrlManager and Apache .htaccess rules to create clean, user-friendly URLs. This guide covers URL configuration, rewrite rules, common issues, and practical tips for improving Yii application routing
Yii
Yii Rights: Get all assigned roles of a user
Learn how to retrieve user roles in Yii Framework using the authassignment table, CDbCommand, and queryAll(). This guide explains how to fetch and store user roles, use parameter binding for safer SQL queries, leverage Yii’s built-in authorization manager, and implement role-based access control in PHP applications.
Yii
Disabled Yii logs and stack trace messages
Open up the index.php from root folder and comment out following 2 lines [code language=”php”] defined(‘YII_DEBUG’) or define(‘YII