Yii2 change username on creation -
i want app detect when username taken , change it. in fact, when user register himself enter last , first name , username lastname.firstname. how detect username taken , how change (add number example) ?
thanks.
so should override beforevalidate()
function, bellow sample code:
/** * @inheritdoc */ public function beforevalidate() { /** code here **/ $isexist = static::find()->where(['username' => $this->username])->count(); if($isexist){ //count total rows similar username $total = static::find()->where(['like','username', "{$this->username}%"])->count(); //we add $total + 1 username have new unique username $this->username .= '-' . ($total+1); } return parent::beforevalidate(); }
Comments
Post a Comment