国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能

2024-05-04 23:41:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能的相關(guān)資料,需要的朋友可以參考下
 

本文為大家分享了yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能的相關(guān)代碼,具體內(nèi)容如下

視圖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 = '注冊(cè)';$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位數(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; }}

以上就是本文的全部?jī)?nèi)容,幫助大家實(shí)現(xiàn)yii2.0驗(yàn)證功能。



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平阴县| 广元市| 大宁县| 法库县| 彭州市| 鄂州市| 昌吉市| 南城县| 阳西县| 洪雅县| 南华县| 甘肃省| 南召县| 阿瓦提县| 宜兰县| 泽普县| 长汀县| 和政县| 桐乡市| 通河县| 五原县| 阳西县| 兴隆县| 崇礼县| 武冈市| 湘西| 喜德县| 阳山县| 垦利县| 泰安市| 资源县| 诸城市| 沈阳市| 景洪市| 铁岭市| 郓城县| 新乡县| 蒲江县| 平和县| 嫩江县| 呼和浩特市|