yii2.0实现验证用户名与邮箱功能


Posted in PHP onDecember 22, 2015

本文为大家分享了yii2.0实现验证用户名与邮箱功能的相关代码,具体内容如下

视图signup.php代码:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */

$this->title = '注册';
$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',
    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
   ]); ?>
    
    <?= $form->field($model, 'username') ?>
    <?= $form->field($model, 'email') ?>
    <?= $form->field($model, 'password')->passwordInput() ?>
    <?= $form->field($model, 'password_compare')->passwordInput() ?>
    
    <div class="form-group">
     <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
    </div>
    
   <?php ActiveForm::end(); ?>
  </div>
 </div>
</div>

控制器SiteController.php

public function actionSignup()
 {
  $model = new SignupForm();
  
  $model->load($_POST);
  if (Yii::$app->request->isAjax) {
   Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
   return \yii\bootstrap\ActiveForm::validate($model);
  }
  
  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,
  ]);
 }

模型SignupForm.php

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
 public $username;
 public $email;
 public $password;
 public $password_compare;

 /**
  * @inheritdoc
  */
 public function rules()
 {
  return [
   ['username', 'filter', 'filter' => 'trim'],
   ['username', 'required'],
   ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '用户名已存在.'],
   ['username', 'string', 'min' => 2, 'max' => 255],

   ['email', 'filter', 'filter' => 'trim'],
   ['email', 'required'],
   ['email', 'email'],
   ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '邮箱名已存在.'],

   [['password', 'password_compare'], 'required'],
   [['password', 'password_compare'], 'string', 'min' => 6, 'max' => 16, 'message' => '{attribute}是6-16位数字或字母'],
   ['password_compare', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致'],
  ];
 }

 /**
  * 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;
 }
}

以上就是本文的全部内容,帮助大家实现yii2.0验证功能。

PHP 相关文章推荐
IP攻击升级,程序改进以对付新的攻击
Nov 23 PHP
解析用PHP实现var_export的详细介绍
Jun 20 PHP
php常用字符串处理函数实例分析
Nov 22 PHP
thinkphp项目部署到Linux服务器上报错“模板不存在”如何解决
Apr 27 PHP
PHP7+Nginx的配置与安装教程详解
May 10 PHP
thinkPHP分组后模板无法加载问题解决方法
Jul 12 PHP
thinkPHP批量删除的实现方法分析
Nov 09 PHP
php基于curl实现的股票信息查询类实例
Nov 11 PHP
CodeIgniter框架基本增删改查操作示例
Mar 23 PHP
php+ajax实现商品对比功能示例
Apr 13 PHP
关于laravel后台模板laravel-admin select框的使用详解
Oct 03 PHP
php 获取音视频时长,PHP 利用getid3 获取音频文件时长等数据
Apr 01 PHP
PHP内核探索之解释器的执行过程
Dec 22 #PHP
PHP内核探索之变量
Dec 22 #PHP
yii2.0使用Plupload实现带缩放功能的多图上传
Dec 22 #PHP
PHP输入流php://input实例讲解
Dec 22 #PHP
服务器迁移php版本不同可能诱发的问题
Dec 22 #PHP
php上传图片并压缩的实现方法
Dec 22 #PHP
PHP实现图片上传并压缩
Dec 22 #PHP
You might like
电脑硬件及电脑配置知识大全
2020/03/17 数码科技
使用PHP实现二分查找算法代码分享
2011/06/24 PHP
php利用新浪接口查询ip获取地理位置示例
2014/01/20 PHP
laravel5使用freetds连接sql server的方法
2018/12/07 PHP
浅谈Laravel中的三种中间件的作用
2019/10/13 PHP
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
常用的Javascript设计模式小结
2015/12/09 Javascript
js添加绑定事件的方法
2016/05/15 Javascript
canvas实现钟表效果
2017/02/13 Javascript
jQuery插件Echarts实现的双轴图效果示例【附demo源码下载】
2017/03/04 Javascript
基于vue.js实现侧边菜单栏
2017/03/20 Javascript
浅谈AngularJS中$http服务的简单用法
2018/05/15 Javascript
监听angularJs列表数据是否渲染完毕的方法示例
2018/11/07 Javascript
python爬虫教程之爬取百度贴吧并下载的示例
2014/03/07 Python
10款最好的Web开发的 Python 框架
2015/03/18 Python
详解Python的单元测试
2015/04/28 Python
python安装numpy&amp;安装matplotlib&amp; scipy的教程
2017/11/02 Python
Numpy中stack(),hstack(),vstack()函数用法介绍及实例
2018/01/09 Python
Python实现分段线性插值
2018/12/17 Python
详解安装mitmproxy以及遇到的坑和简单用法
2019/01/21 Python
python实现列表中最大最小值输出的示例
2019/07/09 Python
Django框架视图介绍与使用详解
2019/07/18 Python
Python对接六大主流数据库(只需三步)
2019/07/31 Python
Python的互斥锁与信号量详解
2019/09/12 Python
Pytorch之保存读取模型实例
2019/12/30 Python
pytorch读取图像数据转成opencv格式实例
2020/06/02 Python
CSS3教程(1):什么是CSS3
2009/04/02 HTML / CSS
如何利用CSS3制作3D效果文字具体实现样式
2013/05/02 HTML / CSS
Canvas 像素处理之改变透明度的实现代码
2019/01/08 HTML / CSS
The North Face北面英国官网:美国著名户外品牌
2017/12/13 全球购物
酒店开业庆典主持词
2014/03/21 职场文书
大学生感恩父母演讲稿
2014/08/28 职场文书
2014超市收银员工作总结
2014/11/13 职场文书
人工作失职检讨书
2015/05/05 职场文书
2016年员工政治思想表现评语
2015/12/02 职场文书
Python Pygame实战之塔防游戏的实现
2022/03/17 Python