yii添加验证码 和重复密码
<?php
namespace frontend\models; use common\models\User;
use yii\base\Model;
use Yii; /**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
public $rePassword;
public $vitifyCode; /**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255], ['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],//格式必须是邮箱
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'], [['password', 'rePassword'], 'required'],
[['password', 'rePassword'], 'string', 'min' => 6],
['rePassword', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码必须一致'], //两次密码必须一致
['vitifyCode', 'captcha'], //验证码验证
];
} public function attributeLabels() //属性labels
{
return [
'username' => '用户名',
'email' => '邮箱',
'password' => '密码',
'rePassword' => '重复密码',
'vitifyCode' => '验证码',
];
} /**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
} return null;
}
}
?>
<?php
namespace frontend\controllers; use Yii;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post', 'get'],
],
],
];
} /**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'upload' => [
'class' => 'frontend\widgets\ueditor\UEditorAction'
], ];
}
/**
* Signs user up.
*
* @return mixed
*/
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
} return $this->render('signup', [
'model' => $model,
]);
}
?>
<?php /* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */ use yii\helpers\Html;
use yii\bootstrap\ActiveForm; $this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
<h1><?= Html::encode($this->title) ?></h1> <p>Please fill out the following fields to signup:</p> <div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?> <?= $form->field($model, 'username') ?> <?= $form->field($model, 'email') ?> <?= $form->field($model, 'password')->passwordInput() ?> <?= $form->field($model, 'rePassword')->passwordInput() ?> <?= $form->field($model, 'vitifyCode')->widget(\yii\captcha\Captcha::className()) ?> //验证码组件调用 <div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div> <?php ActiveForm::end(); ?>
</div>
</div>
</div>
?>
yii添加验证码 和重复密码的更多相关文章
- Yii添加验证码
添加带验证码的登陆: 1.先在模型modules下的LoginForm.php定义一个存储验证码的变量:public $verfyCode: 2.然后在rules()方法里定义:array('veri ...
- yii登陆中添加验证码
1.在SiteController中添加如下代码: /** * Declares class-based actions. */ public function actions() { return ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- Angular企业级开发(9)-前后端分离之后添加验证码
1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...
- cas添加验证码
cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的.但是他的有一个很不好的地方就是不能提升验证码错误 ...
- asp.net添加验证码
1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...
- PHPCMS v9 自定义表单添加验证码验证
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...
- PHPCMS v9 自定义表单添加验证码
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...
- My sql添加远程用户root密码为password
添加远程用户root密码为password grant all privileges on *.* to root@localhost identified by '123321' with gran ...
随机推荐
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- sql生成随机不重复字符串 可指定长度
存储过程: create procedure dbo.GetRandStr () output) AS BEGIN ), ), @ss varchar DECLARE @I INTEGER, @cou ...
- Linux命令区
netstat -nap 查看防火墙开的端口 find 目录 -name 文件名 find /usr/local/ -name a.php find /usr/ -name a* [?[]] g ...
- 【Python】python3 正则爬取网页输出中文乱码解决
爬取网页时候print输出的时候有中文输出乱码 例如: \\xe4\\xb8\\xad\\xe5\\x8d\\x8e\\xe4\\xb9\\xa6\\xe5\\xb1\\x80 #爬取https:// ...
- 30.纯 CSS 创作一个晃动的公告板
原文地址:https://segmentfault.com/a/1190000014983030 感想: 绝对定位+动画 HTML代码: <div class="signboard&q ...
- 《算法》第二章部分程序 part 5
▶ 书中第二章部分程序,加上自己补充的代码,包括利用优先队列进行多路归并和堆排序 ● 利用优先队列进行多路归并 package package01; import edu.princeton.cs.a ...
- 9. 一个list拆分成多个list返回
/** * @Title: splitList * @Description: 1个list分割成多个list * @param targe 原list * @para ...
- hadoop动态添加删除节点datanode及恢复
1. 配置系统环境 主机名,ssh互信,环境变量等 本文略去jdk安装,请将datanode的jdk安装路径与/etc/hadoop/hadoop-evn.sh中的java_home保持一致,版本ha ...
- 机器学习实战之朴素贝叶斯进行文档分类(Python 代码版)
贝叶斯是搞概率论的.学术圈上有个贝叶斯学派.看起来吊吊的.关于贝叶斯是个啥网上有很多资料.想必读者基本都明了.我这里只简单概括下:贝叶斯分类其实就是基于先验概率的基础上的一种分类法,核心公式就是条件概 ...
- day18-列表生成式、迭代器
1.列表生成式,也叫列表推导式 即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式.优点:构造简单,一行完成缺点:不能排错,不能构建复杂的数据结 ...