麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 23:41:17
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了yii2.0實現驗證用戶名與郵箱功能的相關資料,需要的朋友可以參考下
 

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

視圖signup.php代碼:

<?phpuse 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

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位數字或字母'],   ['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驗證功能。



注:相關教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久6国产| 欧美一级黄色影院 | 国产精品一区二av18款 | 国产91对白叫床清晰播放 | 久久久久国产一区二区三区不卡 | 国产午夜精品一区二区三区嫩草 | 欧美一级黄色片免费观看 | 久久久国产精品免费观看 | 免费观看高清视频网站 | 日日鲁夜夜视频热线播放 | 在线a| 久久综合九色综合久久久精品综合 | 国产成人综合在线观看 | 久久密 | 精品麻豆cm视频在线看 | 欧美视屏一区二区 | 99亚洲伊人久久精品影院红桃 | 午夜偷拍视频 | 国产精品久久久久久久亚洲按摩 | 久久精品视频日本 | 一级在线视频 | 国产九九热 | 91av日韩| 特片网久久 | 久久久久久久久久久亚洲 | 国产成人强伦免费视频网站 | 久久久久一本一区二区青青蜜月 | 牛牛热这里只有精品 | 黄色片免费在线 | 日韩精品中文字幕在线播放 | 中文字幕免费在线看 | 免费a级毛片永久免费 | 日韩色视频在线观看 | 久久人人av | 看免费黄色一级片 | 国产一区二精品区在线 | 国产一区二区成人在线 | 高清一区二区在线观看 | 久久综合伊人 | 中文字幕欧美日韩 | 国产正在播放 |