zoukankan      html  css  js  c++  java
  • yii添加验证码 和重复密码

    <?php
    namespace frontendmodels;

    use commonmodelsUser;
    use yiiaseModel;
    use Yii;

    /**
    * Signup form
    */
    class SignupForm extends Model
    {
    public $username;
    public $email;
    public $password;
    public $rePassword;
    public $vitifyCode;

    /**
    * @inheritdoc
    */
    public function rules()
    {
    return [
    ['username', 'filter', 'filter' => 'trim'],
    ['username', 'required'],
    ['username', 'unique', 'targetClass' => 'commonmodelsUser', 'message' => 'This username has already been taken.'],
    ['username', 'string', 'min' => 2, 'max' => 255],

    ['email', 'filter', 'filter' => 'trim'],
    ['email', 'required'],
    ['email', 'email'],//格式必须是邮箱
    ['email', 'string', 'max' => 255],
    ['email', 'unique', 'targetClass' => 'commonmodelsUser', 'message' => 'This email address has already been taken.'],

    [['password', 'rePassword'], 'required'],
    [['password', 'rePassword'], 'string', 'min' => 6],
    ['rePassword', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码必须一致'], //两次密码必须一致
    ['vitifyCode', 'captcha'], //验证码验证
    ];
    }

    public function attributeLabels() //属性labels
    {
    return [
    'username' => '用户名',
    'email' => '邮箱',
    'password' => '密码',
    'rePassword' => '重复密码',
    'vitifyCode' => '验证码',
    ];
    }

    /**
    * 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;
    }
    }
    ?>
    <?php
    namespace frontendcontrollers;

    use Yii;
    use commonmodelsLoginForm;
    use frontendmodelsPasswordResetRequestForm;
    use frontendmodelsResetPasswordForm;
    use frontendmodelsSignupForm;
    use frontendmodelsContactForm;
    use yiiaseInvalidParamException;
    use yiiwebBadRequestHttpException;
    use yiiwebController;
    use yiifiltersVerbFilter;
    use yiifiltersAccessControl;
    /**
    * Site controller
    */
    class SiteController extends Controller
    {
    /**
    * @inheritdoc
    */
    public function behaviors()
    {
    return [
    'access' => [
    'class' => AccessControl::className(),
    'only' => ['logout', 'signup'],
    'rules' => [
    [
    'actions' => ['signup'],
    'allow' => true,
    'roles' => ['?'],
    ],
    [
    'actions' => ['logout'],
    'allow' => true,
    'roles' => ['@'],
    ],
    ],
    ],
    'verbs' => [
    'class' => VerbFilter::className(),
    'actions' => [
    'logout' => ['post', 'get'],
    ],
    ],
    ];
    }

    /**
    * @inheritdoc
    */
    public function actions()
    {
    return [
    'error' => [
    'class' => 'yiiwebErrorAction',
    ],
    'captcha' => [
    'class' => 'yiicaptchaCaptchaAction',
    'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
    ],
    'upload' => [
    'class' => 'frontendwidgetsueditorUEditorAction'
    ],

    ];
    }

    /**
    * Signs user up.
    *
    * @return mixed
    */
    public function actionSignup()
    {
    $model = new SignupForm();
    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,
    ]);
    }

    ?>

    <?php

    /* @var $this yiiwebView */
    /* @var $form yiiootstrapActiveForm */
    /* @var $model frontendmodelsSignupForm */

    use yiihelpersHtml;
    use yiiootstrapActiveForm;

    $this->title = 'Signup';
    $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']); ?>

    <?= $form->field($model, 'username') ?>

    <?= $form->field($model, 'email') ?>

    <?= $form->field($model, 'password')->passwordInput() ?>

    <?= $form->field($model, 'rePassword')->passwordInput() ?>

    <?= $form->field($model, 'vitifyCode')->widget(yiicaptchaCaptcha::className()) ?> //验证码组件调用

    <div class="form-group">
    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
    </div>

    <?php ActiveForm::end(); ?>
    </div>
    </div>
    </div>
    ?>
  • 相关阅读:
    PyQt5 控件学习(一个一个学习之QCommandLinkButton)
    多任务--线程
    PyQt5 控件学习(一个一个学习之QPushButton)
    PyQt5 控件学习(一个一个学习之QAbstractButton)
    再测我心中的事
    花了两天时间,整理了代码,封装了逻辑
    我现在发现,我写代码有严重的问题
    2014年8月2日0时13分22秒
    2014年8月2日15时13分4秒
    交警与货车司机
  • 原文地址:https://www.cnblogs.com/jasonxiaoqinde/p/7508038.html
Copyright © 2011-2022 走看看