Home >Backend Development >PHP Tutorial >Yii2.0 registration verification model and view method call

Yii2.0 registration verification model and view method call

伊谢尔伦
伊谢尔伦Original
2016-12-02 10:32:221149browse

View signup.php code:

<?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 = &#39;注册&#39;; $this->params[&#39;breadcrumbs&#39;][] = $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([
                &#39;id&#39; => &#39;form-signup&#39;,
                &#39;enableAjaxValidation&#39; => true,
                &#39;enableClientValidation&#39; => true,
            ]); ?>
                
                <?= $form->field($model, &#39;username&#39;) ?>
                <?= $form->field($model, &#39;email&#39;) ?>
                <?= $form->field($model, &#39;password&#39;)->passwordInput() ?>
                <?= $form->field($model, &#39;password_compare&#39;)->passwordInput() ?>
                
                <div class="form-group">
                    <?= Html::submitButton(&#39;Signup&#39;, [&#39;class&#39; => &#39;btn btn-primary&#39;, &#39;name&#39; => &#39;signup-button&#39;]) ?>
                </div>
                
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>

Controller 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(&#39;signup&#39;, [ &#39;model&#39; => $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 [
            [&#39;username&#39;, &#39;filter&#39;, &#39;filter&#39; => &#39;trim&#39;],
            [&#39;username&#39;, &#39;required&#39;],
            [&#39;username&#39;, &#39;unique&#39;, &#39;targetClass&#39; => &#39;\common\models\User&#39;, &#39;message&#39; => &#39;用户名已存在.&#39;],
            [&#39;username&#39;, &#39;string&#39;, &#39;min&#39; => 2, &#39;max&#39; => 255],
            [&#39;email&#39;, &#39;filter&#39;, &#39;filter&#39; => &#39;trim&#39;],
            [&#39;email&#39;, &#39;required&#39;],
            [&#39;email&#39;, &#39;email&#39;],
            [&#39;email&#39;, &#39;unique&#39;, &#39;targetClass&#39; => &#39;\common\models\User&#39;, &#39;message&#39; => &#39;邮箱名已存在.&#39;],
            [[&#39;password&#39;, &#39;password_compare&#39;], &#39;required&#39;],
            [[&#39;password&#39;, &#39;password_compare&#39;], &#39;string&#39;, &#39;min&#39; => 6, &#39;max&#39; => 16, &#39;message&#39; => &#39;{attribute}是6-16位数字或字母&#39;],
            [&#39;password_compare&#39;, &#39;compare&#39;, &#39;compareAttribute&#39; => &#39;password&#39;, &#39;message&#39; => &#39;两次密码不一致&#39;],
        ];
    } /**
     * 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;
    }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn