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.