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 相关文章推荐
用在PHP里的JS打印函数
Oct 09 PHP
在PHP中读取和写入WORD文档的代码
Apr 09 PHP
php实现mysql同步的实现方法
Oct 21 PHP
PHP中CURL方法curl_setopt()函数的参数分享
Jan 19 PHP
用mysql_fetch_array()获取当前行数据的方法详解
Jun 05 PHP
编写php应用程序实现摘要式身份验证的方法详解
Jun 08 PHP
php header功能的使用
Oct 28 PHP
php 生成短网址原理及代码
Jan 23 PHP
详解PHP中的Traits
Jul 29 PHP
深入浅析php json 格式控制
Dec 24 PHP
php 在字符串指定位置插入新字符的简单实现
Jun 28 PHP
thinkPHP商城公告功能开发问题分析
Dec 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
新手学PHP之数据库操作详解及乱码解决!
2007/01/02 PHP
Uchome1.2 1.5 代码学习 common.php
2009/04/24 PHP
php 删除一个数组中的某个值.兼容多维数组!
2012/02/18 PHP
PHP中基于ts与nts版本- vc6和vc9编译版本的区别详解
2013/04/26 PHP
PDO操作MySQL的基础教程(推荐)
2017/08/18 PHP
javascript 有趣而诡异的数组
2009/04/06 Javascript
基于javascript实现页面加载loading效果
2020/09/15 Javascript
javascript弹出窗口中增加确定取消按钮
2016/06/24 Javascript
JS 仿支付宝input文本输入框放大组件的实例
2017/11/14 Javascript
vue-image-crop基于Vue的移动端图片裁剪组件示例
2018/08/28 Javascript
超好用的jQuery分页插件jpaginate用法示例【附源码下载】
2018/12/06 jQuery
解决layui laydate 时间控件一闪而过的问题
2019/09/28 Javascript
[01:09]模型精美,特效酷炫!TI9不朽宝藏Ⅰ鉴赏
2019/05/10 DOTA
python批量修改文件名的实现代码
2014/09/01 Python
python数组复制拷贝的实现方法
2015/06/09 Python
Python中atexit模块的基本使用示例
2015/07/08 Python
带你了解python装饰器
2017/06/15 Python
利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
2017/07/30 Python
Pandas:DataFrame对象的基础操作方法
2018/06/07 Python
python xlsxwriter创建excel图表的方法
2018/06/11 Python
Django保护敏感信息的方法示例
2019/05/09 Python
Python实现EM算法实例代码
2020/10/04 Python
Kipling凯浦林美国官网:世界著名时尚休闲包袋品牌
2016/08/24 全球购物
英国露营设备和户外服装购物网站:Simply Hike
2019/05/05 全球购物
Luxplus荷兰:以会员价购买美容产品等,独家优惠
2019/08/30 全球购物
Puma印度官网:德国运动品牌
2019/10/06 全球购物
Linux Interview Questions For software testers
2013/05/17 面试题
软件测试面试题
2015/10/21 面试题
如何用Python来进行查询和替换一个文本字符串
2014/01/02 面试题
食堂员工工作职责
2013/12/18 职场文书
2015年环境监察工作总结
2015/07/23 职场文书
2016年植树节红领巾广播稿
2015/12/17 职场文书
MySQL EXPLAIN输出列的详细解释
2021/05/12 MySQL
企业开发CSS命名BEM代码规范实践
2022/02/12 HTML / CSS
nginx容器方式反向代理实战
2022/04/18 Servers
Python保存并浏览用户的历史记录
2022/04/29 Python