Yii2.0 implements user name and email verification

Source: Internet
Author: User
This article mainly introduces information about how yii2.0 can verify the user name and email function. For more information, see the code of yii2.0 to verify the user name and email function, the details are as follows:

View signup. php code:

<? 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 = 'registration '; $ this-> params ['breadcrumbs '] [] = $ this-> title;?>

<? = Html: encode ($ this-> title)?>

Please fill out the following fields to signup:

<? Php $ form = ActiveForm: begin (['id' => 'form-signup', 'enablesajaxvalidation '=> true, 'enablesclientvalidation' => true,]);?> <? = $ Form-> field ($ model, 'Username')?> <? = $ Form-> field ($ model, 'Email ')?> <? = $ Form-> field ($ model, 'password')-> passwordInput ()?> <? = $ Form-> field ($ model, 'password _ compare ')-> passwordInput ()?>

<? = Html: submitButton ('signup', ['class' => 'btn btn-primary ', 'name' => 'signup-click'])?>

<? Php ActiveForm: end ();?>

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

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', 'requestred'], ['username', 'Unique ', 'targetclass' => '\ common \ models \ user', 'message' =>' the User name already exists. '], ['username', 'string', 'min' => 2, 'Max' => 255], ['email ', 'filter ', 'filter' => 'trim'], ['email ', 'required'], ['email', 'Email '], ['email', 'Unique ', 'targetclass' => '\ common \ models \ user', 'message' =>' The mailbox name already exists. '], [['password', 'password _ compare'], 'requestred'], [['password', 'password _ compare '], 'string ', 'Min' => 6, 'Max '=> 16, 'message' =>' {attribute} is a 6-16 digit or letter '], ['password _ compare ', 'Company', 'companyattribute' => 'password', 'message' => 'inconsistent passwords '],];}/*** 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 ;}}

The above is all the content of this article, helping you implement the yii2.0 verification function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.