激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - yii2.0實現驗證用戶名與郵箱功能

yii2.0實現驗證用戶名與郵箱功能

2020-12-09 15:07PHP教程網 PHP教程

這篇文章主要介紹了yii2.0實現驗證用戶名與郵箱功能的相關資料,需要的朋友可以參考下

本文為大家分享了yii2.0實現驗證用戶名郵箱功能的相關代碼,具體內容如下

視圖signup.php代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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">
 <h1><?= Html::encode($this->title) ?></h1>
 
 <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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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位數字或字母'],
   ['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驗證功能。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 伊人网站 | 精品国产乱码一区二区三区四区 | 蜜桃网在线观看 | 内地av在线 | 久久久久久久久国产精品 | 天堂亚洲一区 | 亚洲性综合网 | 羞羞电影在线观看 | 91精品国产综合久久婷婷香 | 伊久在线 | 国产精品视频一区二区三区四区五区 | 久久国产夫妻视频 | 中文字幕国产一区 | 羞羞视频免费入口网站 | 综合日韩欧美 | 一色屋任你操 | 91高清视频在线观看 | 久久亚洲成人 | 成人黄色短视频在线观看 | 在线1区| 毛片在线免费观看视频 | 亚洲天堂欧美在线 | 成人aaaa免费全部观看 | 色播av在线 | 国产亚洲小视频 | 综合国产一区 | 农村寡妇偷毛片一级 | 中文字幕视频在线播放 | 亚洲精品成人在线视频 | 国产精品久久久不卡 | 国产午夜精品久久久久婷 | 国产精品野外av久久久 | 国产精品久久久久永久免费 | 精品久久久久久国产三级 | 黄色片视频免费观看 | 成人在线视频一区 | 婷婷精品国产一区二区三区日韩 | 成人艳情一二三区 | h色视频网站| 色中色在线播放 | 偿还电影免费 |