YII2 rules validation rule, Ajax verifies that the mobile phone number is unique

Source: Internet
Author: User
Tags yii

<?phpnamespace frontend\models; UseYii; UseYii\base\model;/** * Signup form*/classSignupformextendsmodel{ Public $mobile;  Public $captcha;  Public $password; /** * @inheritdoc*/     Public functionrules () {return [            [[' Mobile ', ' captcha ', ' Password '], ' trim ',            [[' Mobile ', ' captcha ', ' Password '], ' required ',            [[' Mobile ', ' match ', ' pattern ' = '/^1[3|4|5|7|8][0-9]{9}$/'],            [[' Mobile ', ' unique ', ' targetclass ' = ' \common\models\user ', ' message ' = ' The mobile number has been registered! ‘],                        [[' Password ', ' match ', ' pattern ' = '/^\s+$/'],            [[' Password '], ' string ', ' length ' = [6, 32]],                        [[' Captcha '], ' captcha ',        ]; }        /** * @inheritdoc*/     Public functionAttributelabels () {return [            ' Mobile ' + ' phone number ', ' captcha ' = ' Verification code ', ' Password ' + ' password '        ]; }         Public functionwritesession () {$session= Yii::$app-session; $session-open (); $session[' mobilesignuptimeout '] = Time() + 600; $session[' mobilesignup '] =$this-Mobile; $session[' mobilesignuppassword '] =$this-password; }}

usercontroller.php

        if($step!== ' 2 ') {            $model=NewSignupform (); $model->load (Yii::$app->request->post ()); if(Yii::$app->request->Isajax) {Yii::$app->response->format = Response::Format_json; returnActiveform::validate ($model); }                        if(Yii::$app->request->ispost &&$model-Validate ()) {                $model-writesession (); if($this->_sendmsg ($model-mobile)) {Yii:: info ("User registered to send SMS verification code successfully! Phone Number: {$model->mobile} "); Yii::$app->session->setflash (' sentsuccess '); } Else{Yii:: Warning ("User registration to send SMS verification code failed!") Phone Number: {$model->mobile}, Description: ". YII::$app->smser->message); Yii::$app->session->setflash (' failedtosend ', ' verification code failed to send, please try again! ‘); }                return $this->redirect ([' Signup ', ' step ' = ' 2 ')]); }

signup.php

        if($step!== ' 2 ') {            $model=NewSignupform (); $model->load (Yii::$app->request->post ()); if(Yii::$app->request->Isajax) {Yii::$app->response->format = Response::Format_json; returnActiveform::validate ($model); }                        if(Yii::$app->request->ispost &&$model-Validate ()) {                $model-writesession (); if($this->_sendmsg ($model-mobile)) {Yii:: info ("User registered to send SMS verification code successfully! Phone Number: {$model->mobile} "); Yii::$app->session->setflash (' sentsuccess '); } Else{Yii:: Warning ("User registration to send SMS verification code failed!") Phone Number: {$model->mobile}, Description: ". YII::$app->smser->message); Yii::$app->session->setflash (' failedtosend ', ' verification code failed to send, please try again! ‘); }                return $this->redirect ([' Signup ', ' step ' = ' 2 ')]); }

YII2 rules validation rule, Ajax verifies that the mobile phone number is unique

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.