1. 云栖社区>
  2. PHP教程>
  3. 正文

yii2.0 验证用户名唯一与邮箱唯一

作者:用户 来源:互联网 时间:2017-12-01 18:48:50

邮箱验证用户名

yii2.0 验证用户名唯一与邮箱唯一 - 摘要: 本文讲的是yii2.0 验证用户名唯一与邮箱唯一,视图signup.php代码:<?phpuse yii/helpers/Html;use yii/bootstrap/ActiveForm;/* @var $this yii/web/View *//* @var $form yii/

视图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;?><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位数字或字母'],    ['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 验证用户名唯一与邮箱唯一

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索邮箱 , 验证 用户名 ,以便于您获取更多的相关知识。

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一

云服务器9.9元/月,大学必备