溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用yii2.0怎么實(shí)現(xiàn)一個(gè)用戶名驗(yàn)證功能

發(fā)布時(shí)間:2020-12-19 16:10:34 來源:億速云 閱讀:162 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)利用yii2.0怎么實(shí)現(xiàn)一個(gè)用戶名驗(yàn)證功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

視圖signup.php代碼:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */

$this->title = '注冊';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
 <h2><?= Html::encode($this->title) ?></h2>

 <p>Please fill out the following fields to signup:</p>

 <div class="row">
  <div class="col-lg-5">
   <?php $form = ActiveForm::begin([
    'id' => 'form-signup',
    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
   ]); ?>
    
    <?= $form->field($model, 'username') ?>
    <?= $form->field($model, 'email') ?>
    <?= $form->field($model, 'password')->passwordInput() ?>
    <?= $form->field($model, 'password_compare')->passwordInput() ?>
    
    <div class="form-group">
     <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
    </div>
    
   <?php ActiveForm::end(); ?>
  </div>
 </div>
</div>

控制器SiteController.php

public function actionSignup()
 {
  $model = new SignupForm();
  
  $model->load($_POST);
  if (Yii::$app->request->isAjax) {
   Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
   return \yii\bootstrap\ActiveForm::validate($model);
  }
  
  if ($model->load(Yii::$app->request->post())) {
   if ($user = $model->signup()) {
    if (Yii::$app->getUser()->login($user)) {
     return $this->goHome();
    }
   }
  }

  return $this->render('signup', [
   'model' => $model,
  ]);
 }

模型SignupForm.php

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
 public $username;
 public $email;
 public $password;
 public $password_compare;

 /**
  * @inheritdoc
  */
 public function rules()
 {
  return [
   ['username', 'filter', 'filter' => 'trim'],
   ['username', 'required'],
   ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '用戶名已存在.'],
   ['username', 'string', 'min' => 2, 'max' => 255],

   ['email', 'filter', 'filter' => 'trim'],
   ['email', 'required'],
   ['email', 'email'],
   ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '郵箱名已存在.'],

   [['password', 'password_compare'], 'required'],
   [['password', 'password_compare'], 'string', 'min' => 6, 'max' => 16, 'message' => '{attribute}是6-16位數(shù)字或字母'],
   ['password_compare', 'compare', 'compareAttribute' => 'password', 'message' => '兩次密碼不一致'],
  ];
 }

 /**
  * Signs user up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function signup()
 {
  if ($this->validate()) {
   $user = new User();
   $user->username = $this->username;
   $user->email = $this->email;
   $user->setPassword($this->password);
   $user->generateAuthKey();
   if ($user->save()) {
    return $user;
   }
  }

  return null;
 }
}

以上就是利用yii2.0怎么實(shí)現(xiàn)一個(gè)用戶名驗(yàn)證功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI