yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能
本文為大家分享了yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能的相關(guān)代碼,具體內(nèi)容如下
視圖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 = '注冊(cè)';
$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位數(shù)字或字母'],
['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;
}
}
以上就是本文的全部?jī)?nèi)容,幫助大家實(shí)現(xiàn)yii2.0驗(yàn)證功能。
- Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁(yè)插入評(píng)論表單的方法
- Yii框架結(jié)合sphinx,Ajax實(shí)現(xiàn)搜索分頁(yè)功能示例
- Yii2實(shí)現(xiàn)讓關(guān)聯(lián)字段支持搜索功能的方法
- Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法
- yii2.0使用Plupload實(shí)現(xiàn)帶縮放功能的多圖上傳
- 詳細(xì)解讀PHP的Yii框架中登陸功能的實(shí)現(xiàn)
- Yii框架form表單用法實(shí)例
- PHP 基于Yii框架中使用smarty模板的方法詳解
- Yii實(shí)現(xiàn)文章列表置頂功能示例
相關(guān)文章
php數(shù)據(jù)序列化測(cè)試實(shí)例詳解
這篇文章主要介紹了php數(shù)據(jù)序列化測(cè)試實(shí)例詳解的相關(guān)資料,主要介紹msgpack、json、serialize對(duì)比,需要的朋友可以參考下2017-08-08
PHP array_reduce()函數(shù)的應(yīng)用解析
這篇文章主要介紹了PHP array_reduce()的應(yīng)用,本文通過(guò)代碼舉例給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
淺談PHP的數(shù)據(jù)庫(kù)接口和技術(shù)
下面小編就為大家?guī)?lái)一篇淺談PHP的數(shù)據(jù)庫(kù)接口和技術(shù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
thinkphp表單上傳文件并將文件路徑保存到數(shù)據(jù)庫(kù)中
這篇文章主要介紹了thinkphp表單上傳文件并將文件路徑保存到數(shù)據(jù)庫(kù)中的相關(guān)資料,需要的朋友可以參考下2016-07-07
ThinkPHP5.0框架實(shí)現(xiàn)切換數(shù)據(jù)庫(kù)的方法分析
這篇文章主要介紹了ThinkPHP5.0框架實(shí)現(xiàn)切換數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了thinkPHP5.0數(shù)據(jù)庫(kù)的配置與動(dòng)態(tài)連接相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
php使用get和post傳遞數(shù)據(jù)出現(xiàn)414?Request-URI?Too?Large的原因分析及解決方案
Request-URI Too Large(請(qǐng)求URI過(guò)長(zhǎng))是一個(gè)HTTP錯(cuò)誤狀態(tài)碼,表示所發(fā)送的HTTP請(qǐng)求中的URI(統(tǒng)一資源標(biāo)識(shí)符)長(zhǎng)度超過(guò)了服務(wù)器能夠處理的限制,這篇文章主要介紹了php使用get和post傳遞數(shù)據(jù)出現(xiàn)414?Request-URI Too?Large的解決方案,需要的朋友可以參考下2023-08-08
php從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)用ajax傳送到前臺(tái)的方法
今天小編就為大家分享一篇php從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)用ajax傳送到前臺(tái)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Yii框架數(shù)據(jù)模型的驗(yàn)證規(guī)則rules()被執(zhí)行的方法
這篇文章主要介紹了Yii框架數(shù)據(jù)模型的驗(yàn)證規(guī)則rules()被執(zhí)行的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
基于php流程控制語(yǔ)句和循環(huán)控制語(yǔ)句(講解)
下面小編就為大家?guī)?lái)一篇基于php流程控制語(yǔ)句和循環(huán)控制語(yǔ)句(講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10

