国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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驗證功能。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久亚洲国产美女国产盗摄 | 色综合久久88色综合天天6 | www.日韩 | 国产视频久久 | 日韩av一区二区在线观看 | 三级视频网站 | 久久久久久99精品 | 国产精品久久久久久99999 | 1000部精品久久久久久久久 | 国产免费性 | 精品久久精品 | 国产精品久久久 | 国产精品免费在线 | 久久精品小视频 | 日韩视频免费在线播放 | 精品久久久久一区二区国产 | 日韩av免费在线观看 | 亚洲aaa | 国产99久久精品一区二区永久免费 | 黄色成人在线视频 | 综合久 | 欧美成人精品一区二区三区 | 日韩免费在线视频 | 免费一区二区三区 | 免费又黄又爽又色的视频 | 日本看片网站 | 亚洲男女视频在线观看 | www亚洲精品 | 亚洲网视频 | 91精品国产综合久久久久久丝袜 | 欧美日韩一区在线 | 久久国产精品免费 | 久久亚洲精品综合 | 毛片在线免费观看网站 | 欧美成人精品一区二区男人看 | av免费网站在线观看 | 久久久亚洲综合 | 在线播放视频一区 | 91在线中文 | 久久久久综合狠狠综合日本高清 | 免费三片在线观看网站 |