yii2.0实现验证用户名与邮箱功能
发布:smiling 来源: PHP粉丝网 添加日期:2021-06-30 22:02:12 浏览: 评论:0
这篇文章主要介绍了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验证功能。
Tags: yii2 0验证用户名
相关文章
- ·yii2超好用的日期组件和时间组件(2019-07-30)
- ·yii2的ActiveForm表单使用的方法介绍(2020-02-15)
- ·Yii2框架的csrf验证原理分析及token缓存解决方案(2020-04-05)
- ·Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源(2021-03-01)
- ·yii2.0之GridView自定义按钮和链接用法(2021-05-03)
- ·列举PHP的Yii 2框架的开发优势(2021-06-08)
- ·Yii2.0高级框架数据库增删改查的一些操作(2021-06-26)
- ·实例讲解yii2.0在php命令行中运行的步骤(2021-06-27)
- ·yii2.0使用Plupload实现带缩放功能的多图上传(2021-06-30)
- ·yii2中添加验证码的实现方法(2021-07-05)
- ·YII2.0之Activeform表单组件用法实例(2021-07-05)
- ·yii2中使用Active Record模式的方法(2021-07-05)
- ·yii2框架中使用下拉菜单的自动搜索yii-widget-select2实例分析(2021-07-05)
- ·Yii2框架引用bootstrap中日期插件yii2-date-picker的方法(2021-07-05)
- ·yii2中的rules 自定义验证规则详解(2021-07-29)
- ·yii2整合百度编辑器umeditor及umeditor图片上传问题的解决办法(2021-07-29)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)