>  기사  >  백엔드 개발  >  Yii2.0 등록 검증 모델 및 뷰 메소드 호출

Yii2.0 등록 검증 모델 및 뷰 메소드 호출

伊谢尔伦
伊谢尔伦원래의
2016-12-02 10:32:221135검색

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으로 문의하세요.