yii2.0 implements the function of verifying user name and email, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:37
Original
858 people have browsed it

yii2.0 implements the verification of username and email function,

This article shares the relevant code of yii2.0 to implement the verification of username and email function. The specific content is as follows

View signup.php code:

<&#63;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;
&#63;>
<div class="site-signup">
 <h1><&#63;= Html::encode($this->title) &#63;></h1>

 <p>Please fill out the following fields to signup:</p>

 <div class="row">
  <div class="col-lg-5">
   <&#63;php $form = ActiveForm::begin([
    'id' => 'form-signup',
    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
   ]); &#63;>
    
    <&#63;= $form->field($model, 'username') &#63;>
    <&#63;= $form->field($model, 'email') &#63;>
    <&#63;= $form->field($model, 'password')->passwordInput() &#63;>
    <&#63;= $form->field($model, 'password_compare')->passwordInput() &#63;>
    
    <div class="form-group">
     <&#63;= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) &#63;>
    </div>
    
   <&#63;php ActiveForm::end(); &#63;>
  </div>
 </div>
</div>

Copy after login

ControllerSiteController.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,
  ]);
 }

Copy after login

ModelSignupForm.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;
 }
}
Copy after login

The above is the entire content of this article to help you implement the yii2.0 verification function.

Articles you may be interested in:

  • Analysis of yii implementation of creating verification code examples
  • yii user registration form verification examples
  • Tips for PHP YII framework development Custom validation rules for rules in models
  • Comprehensive form validation rules of PHP Yii framework
  • Yii uses ajax validation to display error messagebox solutions
  • In yii Detailed explanation of adding a new user verification method
  • How to use Captcha verification code in Yii

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084569.htmlTechArticleyii2.0 implements the verification of user name and email function. This article shares with you how yii2.0 implements the verification of user name and email. The relevant code for the mailbox function, the specific content is as follows: phpus...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!