• YII2.0 验证表单


    控制器代码

    <?php
    namespace appmodulespubcontrollers;
    use Yii;
    use backendbaseBaseController;
    use backendmodulespubmodelsLoginForm;
    use backendmodulespubmodelsRegisterForm;
    class DefaultController extends BaseController{
        public $layout = false;
        public function actions(){
            return [
                // captcha action renders the CAPTCHA image displayed on the contact page
                'captcha'=>[
                    'class' => 'yiicaptchaCaptchaAction',
                    '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 backendmodulespubmodels;
    use appmodulespubmodelsYiiUser;
    use backendmodelsCommonModel;
    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'=>'ackendmodulespubmodelsYiiUser','message'=>'账号已存在'],
                ['email','unique','targetClass'=>'ackendmodulespubmodelsYiiUser','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(); ?>
  • 相关阅读:
    LINUX操作系统VIM的安装和配置
    Ubuntu 14.04 LTS中怎样安装fcitx中文输入法
    Ubuntu 速配指南:开启3D桌面特效
    在U盘上安装Windows 7的详细步骤
    设置ip地址、掩码、网关、DNS
    U盘安装电脑系统教程
    取消word中所有超链接
    dos命令批处理发送文字到剪贴板
    Word2007:如何在竖版(纵向)页面中间插入横版(横向)页面
    ubuntu12.04 alternate win7 双系统安装
  • 原文地址:https://www.cnblogs.com/wenxinphp/p/5800436.html
Copyright © 2020-2023  润新知