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

 更新时间:2015年12月22日 10:23:22   投稿:lijiao  
这篇文章主要介绍了yii2.0实现验证用户名与邮箱功能的相关资料,需要的朋友可以参考下

本文为大家分享了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实现微信申请退款流程实例代码

    本篇文章给大家介绍php实现微信申请退款流程,使用到官方提供的SDK中的最重要的一个类文件WxPay.Api.php中提供的refund()方法来实现的,完整大家大家参考下本文
    2018-03-03
  • 解决laravel id非自增 模型取回为0 的问题

    解决laravel id非自增 模型取回为0 的问题

    今天小编就为大家分享一篇解决laravel id非自增 模型取回为0 的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • 如何通过View::first使用Laravel Blade的动态模板详解

    如何通过View::first使用Laravel Blade的动态模板详解

    这篇文章主要给大家介绍了关于如何通过View::first使用Laravel Blade的动态模板的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用php具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
    2017-09-09
  • Laravel推荐使用的十个辅助函数

    Laravel推荐使用的十个辅助函数

    这篇文章主要给大家介绍了关于Laravel推荐使用的十个辅助函数,文中通过示例代码介绍的非常详细,对大家学习或者使用Laravel具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • php读取3389的脚本

    php读取3389的脚本

    通过php获取系统3389端口信息的脚本,这里只分享实现方法,不建议非法用途
    2014-05-05
  • laravel5.6中的外键约束示例

    laravel5.6中的外键约束示例

    今天小编就为大家分享一篇laravel5.6中的外键约束示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • php编写的简单页面跳转功能实现代码

    php编写的简单页面跳转功能实现代码

    这篇文章主要介绍了php编写的简单页面跳转功能实现代码,有需要的朋友可以参考一下
    2013-11-11
  • yii2使用GridView实现数据全选及批量删除按钮示例

    yii2使用GridView实现数据全选及批量删除按钮示例

    本篇文章主要介绍了yii2使用GridView实现数据全选及批量删除按钮示例,具有一定的参考价值,有兴趣的可以了解一下。
    2017-03-03
  • thinkPHP框架整合tcpdf插件操作示例

    thinkPHP框架整合tcpdf插件操作示例

    这篇文章主要介绍了thinkPHP框架整合tcpdf插件操作,结合实例形式较为详细的分析了thinkPHP框架整合tcpdf插件的具体步骤、相关操作技巧与注意事项,需要的朋友可以参考下
    2018-08-08
  • php优化查询foreach代码实例讲解

    php优化查询foreach代码实例讲解

    这篇文章主要介绍了php优化查询foreach代码实例讲解,列举了代码实例和测试结果,有感兴趣的同学可以学习下
    2021-03-03

最新评论