首頁  >  文章  >  後端開發  >  yii2.0註冊驗證model與view方法調用

yii2.0註冊驗證model與view方法調用

伊谢尔伦
伊谢尔伦原創
2016-12-02 10:32:221134瀏覽

視圖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 = &#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>

控制器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,
        ]);
    }

模型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;
    }
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn