Yii2 验证手机号、邮箱惟一性

序言

验证惟一性很重要,说不上用得很普及,可是也必需要有。比如注册功能模块,手机号、邮箱注册这些,确定是要验证其的惟一性的,重复了登陆就会混乱。那么如何使用Yii2自带的targetClass验证惟一性呢?使页面刷新的可能不少人都会,要是不刷新页面直接触发targetClass验证的应该就少些了吧!不会的也没必要苦恼,没必要担忧,由于我写这篇文章的目的就是要告诉你怎么经过页面不刷新的方式直接触发targetClass验证其手机号、邮箱惟一性。php

需求分析

一、使用Yii2框架自带的targetClass在不刷新页面的状况下验证手机号、邮箱惟一性。html

效果图

这是点击下一步的时候在页面没刷新的状况下出现的验证提示“手机号已经注册”。
图片描述html5

代码分析

一、配置model,我以注册SignupFrom 的model为例,rules里的代码以下:ajax

public function rules()
    {
         return [
            ['t_mobile', 'filter', 'filter' => 'trim'],
            ['t_mobile', 'required'],
             //targetClass 不会本身调用Ajax验证,提交表单后才会触发
            ['t_mobile', 'unique', 'targetClass' => '\login\models\User', 'message' => '手机号已经注册。'],
            [['t_mobile'],'match','pattern'=>'/^[1][358][0-9]{9}$/'],
            
            ['t_password','required'],
            ['t_re_password','required'],
            ['t_re_password','compare','compareAttribute'=>'t_password','message'=>'两次密码输入不一致。'],
            ['t_password', 'string', 'min' => 6],
            ['t_re_password', 'string', 'min' => 6],
            ['company_id', 'trim'],
            ['t_realname', 'trim'],
            ['company_id', 'required'],
            ['t_realname', 'required'],
            ['company_id', 'string', 'min' => 2, 'max' => 255],
            ['t_realname', 'string', 'min' => 2, 'max' => 255], 
            ['code', 'required'],
            
            //验证码验证,model自定义验证的写法,也分享给你们了
            ['code', function ($attribute, $params) { 
                $cd=SmsCode::find()->where(['mobile'=>$this->t_mobile,'code'=>$this->$attribute])->one();
                if(!empty($cd->code) && (strtolower($this->$attribute) == strtolower($cd->code))){
                    return true;
                }else{  
                    $this->addError('code','验证码错误。');
                }
            }],

        ];
    }

注释:邮箱和手机号的写法是同样的,区别是pattern的验证规则,如是邮箱验证就换成邮箱的正则匹配符。
二、在Controller里加上以下一段Ajax提交表单验证的代码app

//Ajax表单验证
       if((Yii::$app->request->isAjax && $model->load($post))){

            Yii::$app->response->format=Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }

注意:这段代码要放在控制器注册方法的最前面,缘由很简单,不可能你全部的操做都完成了再去走验证吧?
三、View层的Ajax验证触发配置
from表单代码以下:框架

<?php $form = ActiveForm::begin([
                    'action'=>'signup?id=reg',
                    'id' => 'login-form',
                    'validateOnBlur'=>false,//关闭失去焦点验证
                    'enableAjaxValidation'=>true, //开启Ajax验证
                    'enableClientValidation'=>false //关闭客户端验证
 ]); ?>
    <div id="set_up">
        <h3 class="form-title font-blue">设置用户名</h3>
        <div class="alert alert-danger display-hide">
            <button class="close" data-close="alert"></button>
            <span> Enter any username and password. </span>
        </div>

        <div class="form-group  has-error">
            <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<!--            <label class="control-label visible-ie8 visible-ie9">Username</label>-->
            <?= $form->field($model, 't_mobile',['inputOptions'=>['class'=>'form-control form-control-solid placeholder-no-fix','placeholder'=>'手机号']])->label('') ?>

        </div>

        <div class="form-actions">

            <?= Html::Button('下一步', ['class' => 'btn blue uppercase', 'name' => 'login-button','id'=>'next']) ?>
           
            <?= Html::a('登陆',['/site/login'],['class' => 'forget-password']) ?>

        </div>
    </div>

注释:(1)、enableAjaxValidation和enableClientValidation的配置必须是enableAjaxValidation为true,enableClientValidation为false。除此以外id也必需要有。
(2)、上面的点击下一步按钮我使用Button,缘由是个人注册功能不是一步走完的,须要好几步,好几个页面才能完成注册,要实现所有无刷新,不能直接使用submitButton提交表单,得本身写Ajax提交。可是若是大家作的注册功能是一个页面实现的话,那就能够直接用submitButton提交表单了。以上的配置Ajax验证已经生效,提交表单页面不刷新的状况下也能直接触发targetClass验证,没必要担忧验证惟一性的时候页面会刷新了。yii

常见问题

一、只model里配置targetClass惟一性验证,在View层开启enableAjaxValidation和关闭enableClientValidation验证,没有在Controller里边加上文章中我所说的Ajax提交表单验证的代码,致使Ajax验证不成功。那段代码必需要加的。ide

相关资料

一、YII2表单验证问题:注册时ajax验证手机号惟一 :http://www.yiichina.com/question/241post

相关文章
相关标签/搜索