<?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添加验证码 和重复密码的更多相关文章

  1. Yii添加验证码

    添加带验证码的登陆: 1.先在模型modules下的LoginForm.php定义一个存储验证码的变量:public $verfyCode: 2.然后在rules()方法里定义:array('veri ...

  2. yii登陆中添加验证码

    1.在SiteController中添加如下代码: /** * Declares class-based actions. */ public function actions() { return  ...

  3. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  4. Angular企业级开发(9)-前后端分离之后添加验证码

    1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...

  5. cas添加验证码

    cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的.但是他的有一个很不好的地方就是不能提升验证码错误 ...

  6. asp.net添加验证码

    1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...

  7. PHPCMS v9 自定义表单添加验证码验证

    1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  8. PHPCMS v9 自定义表单添加验证码

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

  9. My sql添加远程用户root密码为password

    添加远程用户root密码为password grant all privileges on *.* to root@localhost identified by '123321' with gran ...

随机推荐

  1. v修 pp改n属性

    通过修改DNS达到不FQ也能访问Google(仅限于Google)   一.前言 不知道各位小伙伴们现在用的搜索引擎是用Google搜索还是百度搜索呢?但我个人还是比较极力推荐用Google搜索的,首 ...

  2. SVG 学习<八> SVG的路径——path(2)贝塞尔曲线命令、光滑贝塞尔曲线命令

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  3. ant编译时提示一大堆软件包不存在的问题

    ant编译时提示一大堆软件包不存在的问题  解决方案: 把项目的 lib 里的jar包 放的 jdk的jre的ext的目录下 例如 java home 是 D:JavaEEJavajdk1.8.0_1 ...

  4. django-chunks文件

    with open(file_save_path, 'wb') as f: for chunk in file_content.chunks(): f.write(chunk)

  5. 下载,和scp上传问题

    下载,和scp上传问题 wget http://download.openx.org/openx-2.8.10.zip  [默认当前目录] scp ldm@192.168.1.150:/var/www ...

  6. Mybatis九( mybatis插件的原理及使用)

    1.插件执行原理 一.demo 1.测试类 @Test public void test1() { String resource = "mybatis-config.xml"; ...

  7. LINUX系统备份还原

    1.Linux系统备份工具 REAR (RELAX-AND-RECOVER) https://blog.csdn.net/qq43748322/article/details/78710754?loc ...

  8. 1. 配置win7下odbc数据源找不到数据库驱动的问题

    win7下ODBC数据源DB2的链接 直接在控制面板---管理工具----数据源(ODBC) 打开数据源配置,发现只有SQLServer的驱动,其他的都没有了. 解决方法是C:\Windows\Sys ...

  9. 02.设计模式_NullObject模式

    使用NULL OBJECT模式,我们可以确保返回的总是有效的对象,即使失败时也代表对象什么也不做. 下面以一个数据库查询的示例来演示空对象模式. 1.Employe实体对象空对象的接口 Employe ...

  10. php实现单点登录实例

    1.准备两个虚拟域名 127.0.0.1 www.a.com127.0.0.1 www.b.com 2.在a的根目录下创建以下文件 1 //index.php 2 3 <?php 4 sessi ...