控制器代码

<?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(); ?>

YII2.0 验证表单的更多相关文章

  1. yii2.0 Activeform表单部分组件使用方法

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  2. yii2.0 Activeform表单部分组件使用方法 [ 2.0 版本 ]

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  3. tp框架之自动验证表单

    tp框架的create自动加载表单的方法可以自动根据自己定义的要求来验证表单里面的内容,但是由于是在后台执行代码,会拖慢程序运行速度,所以还是建议通过前端js来进行判断,后台只进行数据库的查询以及传值 ...

  4. ajax验证表单元素规范正确与否 ajax展示加载数据库数据 ajax三级联动

    一.ajax验证表单元素规范正确与否 以用ajax来验证用户名是否被占用为例 1创建表单元素<input type="text" id="t"> 2 ...

  5. js验证表单大全

    js验证表单大全 1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert(" ...

  6. 运用jQuery写的验证表单

    //运用jQuery写的验证表单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  7. 开始使用HTML5和CSS3验证表单

    使用HTML5和CSS3验证表单 客户端验证是网页客户端程序最常用的功能之一,我们之前使用了各种各样的js库来进行表单的验证.HTML5其实早已为我们提供了表单验证的功能.至于为啥没有流行起来估计是兼 ...

  8. H5利用pattern属性和oninvalid属性验证表单

    HTML代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  9. flask 在视图函数中验证表单

    在视图函数中验证表单 因为现在的basic_form视图同时接受两种类型的请求:GET请求和POST请求.所以我们要根据请求方法的不同执行不同的代码.具体来说,首先是实例化表单,如果是GET请求,就渲 ...

随机推荐

  1. Editplus配置VC++(1) 及相关注意事项

    下篇文章:Editplus配置VC++(2) 与/d1reportSingleClassLayout 原本用的是VC++2010 现在换成了Visual Studio 2013,editplus相关配 ...

  2. Happy Number - LeetCode

    examination questions Write an algorithm to determine if a number is "happy". A happy numb ...

  3. Message Queue vs. Web Services?

    From stackoverflow.com When you use a web service you have a client and a server: If the server fail ...

  4. 学习mongo系列(十一)关系

    准备工作:首先在maxh数据库的address集合中先插入数据 > db.address.insert({child_address:"gansu"}) WriteResul ...

  5. 与Status Bar和Navigation Bar相关的一些东西

    Android Navigation Bar Status Bar   与StatusBar和NavigationBar相关的东西有两种,一是控制它们的显示与隐藏,二是控制它们的透明与否及背景. 在2 ...

  6. Xcode创建Object-C程序

    一. Xcode 环境安装 与 工程创建 1. 下载环境 相关资源下载 : -- IOS 相关资料下载页面 :  https://developer.apple.com/devcenter/ios/i ...

  7. 一个php soap的错误记录

    今天使用php soap实现两个系统之间的互通 在写好php的soapserver后,client端调用一直报 looks like we got no XML document,尝试好久后无法解决 ...

  8. EaseType缓动函数

    http://sol.gfxile.net/interpolation/   一篇很详细的图文

  9. [笔记]使用clearfix清除浮动

    转载自奶牛博客 .clearfix { *zoom: 1; } .clearfix:before, .clearfix:after { display: table; line-height: 0; ...

  10. Android开发--LinearLayout的应用

    1.简介 LinearLayout为安卓三大常用布局中的线性布局.其中,线性布局又分为水平线性布局和垂直线性布局.视图如下所示: