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 1.ExecutorService四种线程池的例子与说明
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...
- TessorFlow学习 之 神经网络的构建
1.建立一个神经网络添加层 输入值.输入的大小.输出的大小和激励函数 学过神经网络的人看下面这个图就明白了,不懂的去看看我的另一篇博客 def add_layer(inputs , in_size , ...
- Docker Basic
1.简介 last 1.简介 1.1目的?一次编译.到处运行: 1.2.是什么? 实现[运行环境和配置文件的]软件容器,方便[持续集成]实现[整体发布]的容器虚拟化技术: 概括:整体发布的[容器虚拟化 ...
- Strandbeest mechanism and Leg mechanism
I have to say besides computer science study, I'm also interested in Leg mechanism. Share two keywor ...
- FreeMarker生成Word文档
FreeMarker简介: FreeMarker是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具,它不是面向最终用户的,而是一个 ...
- iOS重写和成员变量访问权限
一.重写机制 1.覆盖父类的方法 2.对父类方法做进一步的补充 注意:父类声明过得方法,子类无需声明. * 子类如果重写了父类的方法: 1.父类的指针指向子类的对象,则调用方法时,调用的是子类的方法: ...
- Oracle数据库基础教程
Oracle基础 简介 数据库实例 表空间 登录身份和角色 用户和授权 数据类型 数据操作 导入数据库 一.Oracle基础: 1.简介 Oracle创建数据库不能像SQL Server那样用一个简单 ...
- JavaScript 字符串拼接 & setInterval()实现简单动画
在学习JavaScript DOM编程艺术第十章时,遇到了一个小问题: 想要实现的最终效果:一个小方块不断的向下移动. 小方块绝对定位,设置好top与left值后,写了一个动态获取方块到上方距离并在每 ...
- stage.focus后 有黄色边框怎么去掉
stage.stageFocusRect = false; stage.focus=niao; 必须先设为false
- 小麦成长记-<专业盗图好几年>
========================================图片来源朋友圈的朋友~