>  기사  >  백엔드 개발  >  yii2.0实现验证用户名与邮箱功能,_PHP教程

yii2.0实现验证用户名与邮箱功能,_PHP教程

WBOY
WBOY원래의
2016-07-12 09:02:37846검색

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

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

视图signup.php代码:

<&#63;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;
&#63;>
<div class="site-signup">
 <h1><&#63;= Html::encode($this->title) &#63;></h1>

 <p>Please fill out the following fields to signup:</p>

 <div class="row">
  <div class="col-lg-5">
   <&#63;php $form = ActiveForm::begin([
    'id' => 'form-signup',
    'enableAjaxValidation' => true,
    'enableClientValidation' => true,
   ]); &#63;>
    
    <&#63;= $form->field($model, 'username') &#63;>
    <&#63;= $form->field($model, 'email') &#63;>
    <&#63;= $form->field($model, 'password')->passwordInput() &#63;>
    <&#63;= $form->field($model, 'password_compare')->passwordInput() &#63;>
    
    <div class="form-group">
     <&#63;= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) &#63;>
    </div>
    
   <&#63;php ActiveForm::end(); &#63;>
  </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验证功能。

您可能感兴趣的文章:

  • yii实现创建验证码实例解析
  • yii用户注册表单验证实例
  • PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
  • PHP Yii框架之表单验证规则大全
  • Yii使用ajax验证显示错误messagebox的解决方法
  • 在yii中新增一个用户验证的方法详解
  • Yii使用Captcha验证码的方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1084569.htmlTechArticleyii2.0实现验证用户名与邮箱功能, 本文为大家分享了yii2.0实现验证用户名与邮箱功能的相关代码,具体内容如下 视图signup.php代码: phpus...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.