• yii2 rules验证规则,ajax验证*是否唯一


    <?php
    
    namespace frontendmodels;
    
    use Yii;
    use yiiaseModel;
    
    /**
     * Signup form
     */
    class SignupForm extends Model
    {
        public $mobile;
        public $captcha;
        public $password;
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['mobile', 'captcha', 'password'], 'trim'],
                [['mobile', 'captcha', 'password'], 'required'],
    
                [['mobile'], 'match', 'pattern' => '/^1[3|4|5|7|8][0-9]{9}$/'],
                [['mobile'], 'unique', 'targetClass' => 'commonmodelsUser', 'message' => '该手机号已被注册!'],
                
                [['password'], 'match', 'pattern' => '/^S+$/'],
                [['password'], 'string', 'length' => [6, 32]],
                
                [['captcha'], 'captcha'],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'mobile' => '手机号',
                'captcha' => '验证码',
                'password' => '密码'
            ];
        }
        
        public function writeSession()
        {
            $session = Yii::$app->session;
            $session->open();
            $session['mobileSignupTimeout'] = time() + 600;
            $session['mobileSignup'] = $this->mobile;
            $session['mobileSignupPassword'] = $this->password;
        }
    }

    UserController.php

            if ($step !== '2') {
                $model = new SignupForm();
                $model->load(Yii::$app->request->post());
                
                if (Yii::$app->request->isAjax) {
                    Yii::$app->response->format = Response::FORMAT_JSON;
                    return ActiveForm::validate($model);
                }
                
                if (Yii::$app->request->isPost && $model->validate()) {
                    $model->writeSession();
                    if ($this->_sendMsg($model->mobile)) {
                        Yii::info("用户注册发送短信验证码成功!手机号:{$model->mobile}");
                        Yii::$app->session->setFlash('sentSuccess');
                    } else {
                        Yii::warning("用户注册发送短信验证码失败!手机号:{$model->mobile},说明:" . Yii::$app->smser->message);
                        Yii::$app->session->setFlash('failedToSend', '验证码发送失败,请您再试一次!');
                    }
                    return $this->redirect(['signup', 'step' => '2']);
                }

    signup.php

            if ($step !== '2') {
                $model = new SignupForm();
                $model->load(Yii::$app->request->post());
                
                if (Yii::$app->request->isAjax) {
                    Yii::$app->response->format = Response::FORMAT_JSON;
                    return ActiveForm::validate($model);
                }
                
                if (Yii::$app->request->isPost && $model->validate()) {
                    $model->writeSession();
                    if ($this->_sendMsg($model->mobile)) {
                        Yii::info("用户注册发送短信验证码成功!手机号:{$model->mobile}");
                        Yii::$app->session->setFlash('sentSuccess');
                    } else {
                        Yii::warning("用户注册发送短信验证码失败!手机号:{$model->mobile},说明:" . Yii::$app->smser->message);
                        Yii::$app->session->setFlash('failedToSend', '验证码发送失败,请您再试一次!');
                    }
                    return $this->redirect(['signup', 'step' => '2']);
                }
  • 相关阅读:
    lintcode:Palindrome Partitioning 分割回文串
    lintcode:Add Binary 二进制求和
    lintcode :Count 1 in Binary 二进制中有多少个1
    lintcode : 二叉树的最小深度
    lintcode :二叉树的最大深度
    lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
    lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
    lintcode:二叉树的中序遍历
    lintcode:Binary Search 二分查找
    lintcode:1-10题
  • 原文地址:https://www.cnblogs.com/xiong63/p/6728000.html
Copyright © 2020-2023  润新知