yii2.0實現驗證使用者名稱與郵箱功能_php執行個體

來源:互聯網
上載者:User
本文為大家分享了yii2.0實現驗證使用者名稱與郵箱功能的相關代碼,具體內容如下

視圖signup.php代碼:

<?phpuse 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;?> 

<?= Html::encode($this->title) ?>

Please fill out the following fields to signup:

<?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() ?> <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> <?php ActiveForm::end(); ?>

控制器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驗證功能。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.