欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

yii2.0实现验证用户名与邮箱功能

程序员文章站 2022-06-16 14:37:26
本文为大家分享了yii2.0实现验证用户名与邮箱功能的相关代码,具体内容如下 视图signup.php代码:

本文为大家分享了yii2.0实现验证用户名与邮箱功能的相关代码,具体内容如下

视图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 = '注册';
$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验证功能。