yii2.0注册验证model与view方法调用

伊谢尔伦
Lepaskan: 2023-03-02 20:36:01
asal
1138 orang telah melayarinya

视图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>
Salin selepas log masuk

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

模型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;
    }
}
Salin selepas log masuk


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan