警匪 2012-02-17 16:20:21 3698次浏览 6条回复 0 0 0

这是 userController

public function actionRegister()
{
    $model = new User('register');
    if(isset($_POST['User'])) {
        $model->attributes = $_POST['User'];
        $model->validate();
        $model->getErrors();
        if($model->save()) {
            $this->redirect(array('user/login'));
        }
    }
    $this->render('register', array('model' => $model));
}

model中的

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('email, password, passwordConfirm, nick_name, verifyCode', 'required', 'on'=> 'register'),
        array('email, nick_name', 'unique'),
        array('passwordConfirm', 'compare', 'compareAttribute' => 'password'),
        array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
        array('email', 'length', 'min' => '5', 'max' => 255),
        array('password', 'length', 'min' => '4', 'max' => 18),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        //array('user_id, email, password, nick_name, last_login, create', 'safe', 'on'=>'search'),
    );
}
public function beforeSave() {
    if($this->isNewRecord) {
        $this->salt = $this->verifyCode;
        $this->password = md5($this->salt . $this->password);
        $this->create = time();
    }
}

如果我写出来的不够明确,请提出来,我再补

  • 回复于 2012-02-17 16:21 举报

    视图代码看一下!

  • 回复于 2012-02-17 16:22 举报

    这是视图

    <div class="form">
    <?php
    echo CHtml::beginForm();
    ?>
    <div class="row">
        <?php echo CHtml::activeLabelEx($model, 'email'); ?>
        <?php echo CHtml::activeTextField($model, 'email', array('size' => 30, 'maxlength' => 100)); ?>
        <?php echo CHtml::error($model, 'email'); ?>
    </div>
    <div class="row">
        <?php echo CHtml::activeLabelEx($model, 'password'); ?>
        <?php echo CHtml::activePasswordField($model, 'password', array('size' => 30, 'maxlength' => 100))?>
        <?php echo CHtml::error($model, 'password'); ?>
    </div>
    <div class="row">
        <?php echo CHtml::activeLabelEx($model, 'passwordConfirm'); ?>
        <?php echo CHtml::activePasswordField($model, 'passwordConfirm', array('size' => 30, 'maxlength' => 100))?>
        <?php echo CHtml::error($model, 'passwordConfirm'); ?>
    </div>
    <div class="row">
        <?php echo CHtml::activeLabelEx($model, 'nick_name'); ?>
        <?php echo CHtml::activeTextField($model, 'nick_name', array('size' => 30, 'maxlength' => 100))?>
        <?php echo CHtml::error($model, 'nick_name'); ?>
    </div>
    <div class="row">
        <?php $this->widget('CCaptcha'); ?>
        <?php echo CHtml::activeLabelEx($model, 'verifyCode'); ?>
        <?php echo CHtml::activeTextField($model, 'verifyCode', array('size' => 8, 'maxlength' => 10))?>
    </div>
    <div class="row">
        <?php echo CHtml::submitButton('注册', array('name' => 'submit'))?>
    </div>
    <?php echo CHtml::endForm(); ?>
    </div>
    
  • 回复于 2012-02-17 16:26 举报

    舰长啊。我发了。。。

  • 回复于 2012-02-17 16:35 举报

    为什么不用gii生成代码?

  • 回复于 2012-02-17 16:37 举报

    还没弄清,先头生成个form,没生成出来,就没搞

  • 回复于 2012-02-17 17:48 举报

    问题解决了,我把 beforSave 方法定义成了 public,改成这样就好了

    protected function beforeSave() {
        if($this->isNewRecord) {
            $this->salt = $this->verifyCode;
            $this->password = md5($this->salt . $this->password);
            $this->create = time();
        }             
        return parent::beforeSave();
    }
    
您需要登录后才可以回复。登录 | 立即注册