YII2.0 驗證表單


控制器代碼

<?php namespace app\modules\pub\controllers; use Yii; use backend\base\BaseController; use backend\modules\pub\models\LoginForm; use backend\modules\pub\models\RegisterForm; class DefaultController extends BaseController{ public $layout = false; public function actions(){ return [ // captcha action renders the CAPTCHA image displayed on the contact page 'captcha'=>[ 'class' => 'yii\captcha\CaptchaAction', 'backColor'=>0xFFFFFF, //背景顏色 'minLength'=>6, //最短為4位 'maxLength'=>6, //是長為4位 'transparent'=>true, //顯示為透明 'testLimit'=>0, 'fixedVerifyCode' => YII_ENV_TEST ? 'test' : null, ], ]; } //登錄 public function actionIndex(){ if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } else { return $this->render('index', [ 'model' => $model, ]); } } //注冊 public function actionRegister(){ $model = new RegisterForm(); if ($model->load(Yii::$app->request->post())) { if ($user = $model->register()) { if (Yii::$app->getUser()->login($user)) { return $this->goHome(); } } } return $this->render('register', [ 'model' => $model, ]); } } 

模型代碼

<?php namespace backend\modules\pub\models; use app\modules\pub\models\YiiUser; use backend\models\CommonModel; use Yii; class RegisterForm extends CommonModel{ public $username; public $password; public $password_rep; public $email; public $verifyCode; //驗證規則 public function rules(){ return [ //賬號、密碼、確認密碼、郵箱、驗證碼必須 [['username','password','password_rep','email','verifyCode'],'required'], //賬號只能是漢字/數字/下划線,不能包含空格 ['username','match','pattern'=>'/^[\x{4e00}-\x{9fa5}_a-zA-Z0-9]*$/'], //用戶名最大10位,最小3位 ['username','string','max'=>16,'min'=>2], //用戶名/郵箱唯一 ['username','unique','targetClass'=>'\backend\modules\pub\models\YiiUser','message'=>'賬號已存在'], ['email','unique','targetClass'=>'\backend\modules\pub\models\YiiUser','message'=>'郵箱已被綁定'], //去除空格 [['username','email'],'trim'], //密碼最大16位,最小6位 ['password','string','max'=>16,'min'=>6], //驗證郵箱 ['email','email','message'=>'郵箱不規范'], //驗證兩次密碼是否一致 ['password_rep','compare','compareAttribute'=>'password','message'=>'兩次密碼不一致'], //驗證碼 ['verifyCode', 'captcha','captchaAction'=>'pub/default/captcha'], ]; } public function attributeLabels(){ return [ 'username'=>'用戶名', 'password'=>'密碼', 'password_rep'=>'確認密碼', 'verifyCode'=>'驗證碼', 'email'=>'郵箱', ]; } public function register(){ if ($this->validate()) { $user = new YiiUser(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { return $user; } } return null; } } 

頁面Form代碼

<?php $form = ActiveForm::begin([ 'id' => 'form-signin', ]); ?> <!--<form id="form-signin" class="form-signin">--> <section> <?= $form->field($model,'username')->textInput(['placeholder'=>'只能由漢字/數字/下划線組成,不能包含空格'])->label('賬號')?> <!--<div class="input-group"> <?/*= $form->field($model,'username')->label('賬號')*/?> <input type="text" class="form-control" name="username" placeholder="用戶名"> <div class="input-group-addon"><i class="fa fa-user"></i></div> </div>--> <?= $form->field($model,'password')->passwordInput(['placeholder'=>'密碼'])->label('密碼')?> <?= $form->field($model,'password_rep')->passwordInput(['placeholder'=>'確認密碼'])->label('確認密碼')?> <?= $form->field($model,'email')->textInput(['placeholder'=>'郵箱'])->label('郵箱')?> <?= $form->field($model, 'verifyCode')->label('驗證碼')->widget(Captcha::className(), [ 'options'=>['placeholder'=>'驗證碼'], 'captchaAction' => 'default/captcha', 'imageOptions'=>['style'=>'margin-top:-5px;',], 'template' => '<div class="row"><div class="col-lg-6">{input}</div><div class="col-lg-6">{image}</div></div>', ]) ?> <!--<div class="input-group"> <input type="password" class="form-control" name="password" placeholder="密碼"> <div class="input-group-addon"><i class="fa fa-key"></i></div> </div>--> </section> <section class="controls"> <div class="checkbox check-transparent"> <!--<input type="checkbox" value="1" id="remember" checked> <label for="remember">記住我</label>--> </div> <a href="#">忘記密碼?</a> </section> <section class="log-in"> <?= Html::submitButton('注冊', ['class' => 'btn btn-greensea']) ?> <!--<button class="btn btn-greensea">登錄</button>--> <span>或</span> <?= Html::a('登錄',null,['href'=>Url::toRoute('default/index'),'class'=>'btn btn-slategray']) ?> <!--<button class="btn btn-slategray">創建一個新賬號</button>--> </section> <!--</form>--> <?php ActiveForm::end(); ?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM