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

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -