<?php

namespace frontend\models;

use Yii;
use yii\base\Model; /**
* 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' => '\common\models\User', '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']);
}

yii2 rules验证规则,ajax验证手机号码是否唯一的更多相关文章

  1. MVC验证05-自定义验证规则、验证2个属性值不等

    原文:MVC验证05-自定义验证规则.验证2个属性值不等 本文体验2个属性值不等.即当一个属性输入值,另外一个属性输入的值不能和第一个属性值相等.相关文章包括: MVC验证01-基础.远程验证   M ...

  2. laravel5.5 自定义验证规则——手机验证RULE

    相信很多小伙伴和我一样烦恼,laravel没有自带手机号的验证,每次验证手机号都要写正则这类的规则,每次都是repeat yourself!违背了编码的一个原则,就是Don't repeat your ...

  3. Sping POJO中如何添加验证规则和验证消息提示

    1.示例,验证规则和错误提示作为注解写在声明变量的地方 @NotNull private long id; @NotNull @Size(min = 2, max = 30,message=" ...

  4. ASP.NET MVC验证 - 自定义验证规则、验证2个属性值不等【待验证】

    提示:保存后才提示错误信息 自定义验证特性,继承ValidationAttribute并实现IClientValidatable 这次重写了基类的IsValid()方法的另外一个重载,因为该重载包含了 ...

  5. Django_Form验证(二),ajax验证

    还是一个简单的html提交页面,ajax提交就不需要form表单了: <p><input id="a" type="text" name=&q ...

  6. JQuery validate验证规则

    //定义中文消息 var cnmsg = { required: “必选字段”, remote: “请修正该字段”, email: “请输入正确格式的电子邮件”, url: “请输入合法的网址”, d ...

  7. easyui的validatebox重写自定义验证规则的几个实例

    validatebox已经实现的几个规则: 验证规则是根据使用需求和验证类型属性来定义的,这些规则已经实现(easyui API): email:匹配E-Mail的正则表达式规则. url:匹配URL ...

  8. ThinkPhp5.0模型验证规则

    Tp5提供了模型数据规则的验证功能,用于在数据save或者update前验证数据的有效性.Tp5提供校验规则的类为\Think\Validate,默认提供的校验规则可以查看该文件. 在Model文件中 ...

  9. Thinkphp 1.验证规则 2.静态定义 3.动态验证

    一.验证规则 数据验证可以对表单中的字段进行非法的验证操作.一般提供了两种验证方式: 静态定 义($_validate 属性)和动态验证(validate()方法). //验证规则 array( ar ...

  10. [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则

    目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...

随机推荐

  1. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

  2. 第二章 构建布局良好的windows程序

    工具箱→菜单和工具栏 菜单栏 MenuStrip的类型 MenuItem:菜单项 TextBox:文本框 ComboBoX:组合框 Separato:分割线 前面都有ToolStrip做前缀 Appl ...

  3. SpringBoot-YML的用法

    SpringBoot yml 使用 SpringBoot 默认读取 application.yml|properties YML 比properties配置文件更加节约 简约(结构) 创建applic ...

  4. 20180925 SQL Server游标使用

    之前写了一个存储过程 目标: 根据时间,获取指定范围的数据集A, 再从数据集A 中,取出每行数据中的两个时间,卡另外一个B表里面的某列时间范围的值集.得到均值进行配置值比对. 这里有个问题,就是取得B ...

  5. Python3学习之路~3.2 递归、函数式编程、高阶函数、匿名函数、嵌套函数

    1 递归 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. def calc(n): print(n) if int(n / 2) == 0: return n r ...

  6. C# install-package:"xx"已拥有为“xxx”定义的依赖项

    可能 nuget自身的版本落后于适配程序包的版本 Visual Studio 2013 更新 NuGet 包管理器 Ø  前言 使用 Visual Studio 中的 NuGet 包管理器下载程序时, ...

  7. PinyinUtil

    import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex. ...

  8. iot-hub物管理bug

    物管理中,物绑定证书,如果证书被删除,将会出错 初始化用到 证书编码,证书为null时,null.code报错

  9. yum 安装报错 File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:

    原因: 这是因为yum采用python作为命令解释器,这可以从/usr/bin/yum文件中第一行#!/usr/bin/python发现.而python版本之间兼容性不太好,使得2.X版本与3.0版本 ...

  10. leetcode 300最长上升子序列

    用递归DFS遍历所有组合肯定积分会超时,原因是有很多重复的操作,可以想象每次回溯后肯定会有重复操作.所以改用动态规划.建立一个vector<int>memo,初始化为1,memo[i]表示 ...