手机
当前位置:查字典教程网 >编程开发 >php教程 >yii2.0实现验证用户名与邮箱功能
yii2.0实现验证用户名与邮箱功能
摘要:本文为大家分享了yii2.0实现验证用户名与邮箱功能的相关代码,具体内容如下视图signup.php代码:title='注册';$this-...

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

视图signup.php代码:

<?php use yiihelpersHtml; use yiibootstrapActiveForm; /* @var $this yiiwebView */ /* @var $form yiibootstrapActiveForm */ /* @var $model frontendmodelsSignupForm */ $this->title = '注册'; $this->params['breadcrumbs'][] = $this->title; ?> <div> <h1><?= Html::encode($this->title) ?></h1> <p>Please fill out the following fields to signup:</p> <div> <div> <?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> <?= 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 = yiiwebResponse::FORMAT_JSON; return yiibootstrapActiveForm::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 commonmodelsUser; use yiibaseModel; 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' => 'commonmodelsUser', 'message' => '用户名已存在.'], ['username', 'string', 'min' => 2, 'max' => 255], ['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'unique', 'targetClass' => 'commonmodelsUser', '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验证功能。

【yii2.0实现验证用户名与邮箱功能】相关文章:

第十四节 命名空间 [14]

PHP验证信用卡卡号是否正确函数

Yii实现自动加载类地图的方法

用PHP实现登陆验证码(类似条行码状)

php实现的mongodb操作类

PHP解密Unicode及Escape加密字符串

用libTemplate实现静态网页的生成

php实现的IMEI限制的短信验证码发送类

PHP结合jQuery.autocomplete插件实现输入自动完成提示的功能

PHP实现的功能是显示8条基色色带

精品推荐
分类导航