最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年12月22日 10:23:22   投稿:lijiao  
這篇文章主要介紹了yii2.0實(shí)現(xiàn)驗(yàn)證用戶(hù)名與郵箱功能的相關(guān)資料,需要的朋友可以參考下

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

視圖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 = '注冊(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' => '用戶(hù)名已存在.'],
   ['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)文章

最新評(píng)論

孙吴县| 井研县| 开原市| 蓝田县| 德安县| 三台县| 察隅县| 涞水县| 门头沟区| 樟树市| 云南省| 襄垣县| 定兴县| 台北县| 水城县| 梧州市| 日照市| 阳信县| 醴陵市| 麻江县| 琼结县| 班戈县| 拉萨市| 谢通门县| 南漳县| 寿光市| 武义县| 海安县| 新巴尔虎左旗| 澄城县| 高安市| 沙湾县| 普安县| 四平市| 锦屏县| 调兵山市| 布拖县| 昌乐县| 周至县| 平遥县| 合川市|