yii2.0 implementing validation of username and mailbox features, _php tutorial

Source: Internet
Author: User

yii2.0 to verify the user name and mailbox functionality,


This article for everyone to share the yii2.0 implementation to verify the user name and mailbox functions related code, 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 = ' register '; $this->params[' Breadcrumbs ' [] = $this->title;? > <?= html::encode ($this->title)?> 

Please fill out the following fields to signup:

<? PHP $form = Activeform::begin ([' id ' = ' form-signup ', ' enableajaxvalidation ' = = True, ' Enableclientvalidat Ion ' = = true,]);?> <?= $form->field ($model, ' username ')?> <?= $form->field ($model, ' em Ail ')?> <?= $form->field ($model, ' password ')->passwordinput ()?> <?= $form->field ($model, ' pas Sword_compare ')->passwordinput ()?> <?= Html::submitbutton (' Signup ', [' class ' = ' btn btn-primary ' , ' name ' = ' Signup-button '])?> <?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; PU Blic $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 already exists. '], [' username ', ' string ', ' min ' = 2, ' max ' = + 255], [' email ', ' filter ', ' filter ' = ' trim '], [' email ', ' required '], [' E Mail ', ' email '], [' email ', ' unique ', ' targetclass ' = ' \common\models\user ', ' message ' + ' mailbox name already exists. '], [[' Passwor d ', ' password_compare ', ' required '], [[' Password ', ' Password_compare '], ' string ', ' min ' = 6, ' max ' = +, ' Messa GE ' = = ' {attribute} is 6-16 digits or letters '], [' password_compare ', ' compare ', ' compareattribute ' = ' password ', ' message ' =&G T ' Two times password inconsistent '];  }/** * 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 the entire content of this article, to help you achieve yii2.0 verification function.

Articles you may be interested in:

    • Yii implementation to create verification code instance resolution
    • Yii User Registration Form Validation instance
    • PHP YII Framework Development Little tricks Model (models) Rules custom validation rule
    • PHP Yii Framework's form validation rules Daquan
    • Yii uses AJAX validation to display error MessageBox solutions
    • Add a new user authentication method in Yii
    • Methods of using CAPTCHA verification code for YII

http://www.bkjia.com/PHPjc/1084569.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084569.html techarticle yii2.0 Implementation to verify the user name and mailbox features, this article for everyone to share the yii2.0 implementation of authentication user name and mailbox function related code, the following view signup.php code: Phpus ...

  • Related Article

    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.