• 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']);
                }
  • 相关阅读:
    并行计算的技术路径
    Qt 中文编码问题记录
    rest_rpc 编译安装和测试 ubuntu18.04
    Qt QPorcess 启动外部程序失败的原因之一
    ubuntu 下 cesium的环境搭建
    Qt 渐变色笔记
    Qt编写的自定义控件为什么在QtDesigner中可见,在QtCreator中不可见
    Qt 编译及自动部署 库 工具集(自动复制生成的库及头文件到指定的安装路径)
    Windows10 OSG 编译安装及集成至Qt
    百度图像识别SDK简单使用
  • 原文地址:https://www.cnblogs.com/xiong63/p/6728000.html
Copyright © 2020-2023  润新知