南龙 2012-02-09 15:00:20 3119次浏览 0条回复 0 0 0

无法进行数据入库 actionReg 注册方法

public function actionReg()
{
    $Model=new Reg();
    $this->render('reg',array('model'=>$Model));
		
}

reg.php 模板页面

<div class="form">
<?php 
/*
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'blog-form',
    'enableAjaxValidation'=>false,
));
*/ 

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'Regform',
    'enableClientValidation'=>true,
    'enableAjaxValidation' => true,
    'action'=>Yii::app()->createUrl('blog/check'),
    'clientOptions' => array(
        'validateOnSubmit' => true,		
    ),
    'focus' => array($model, 'reg'),
));
?>
<p class="note">带有<span class="required">*</span>字段是必填选项</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo "用 户  名 ".$form->textField($model,'user',array('size'=>16,'maxlength'=>16)); ?>
<?php echo $form->error($model,'user'); ?>
</div>
<div class="row">
<?php echo "密 &nbsp; &nbsp; 码 ".$form->passwordField($model,'password',array('size'=>16,'maxlength'=>16)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo "重复密码 ".$form->passwordField($model,'password2',array('size'=>16,'maxlength'=>16)); ?>
<?php echo $form->error($model,'password2'); ?>
</div>
	
<div class="row">
    <?php echo "E-mail ".$form->textField($model,'email',array('size'=>16,'maxlength'=>32)); ?>
    <?php echo $form->error($model,'email'); ?>
</div>
<div class="row buttons">
    <?php echo CHtml::submitButton('注册'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->

reg.php 这个是验证模型

<?php
class Reg extends CFormModel
{
    public $user;
    public $password;
    public $password2;
    public $email;
	
    public function rules()
    {
        return array(
            array('user,email,password,password2','required','message'=>'不允许为空'),
            array('user','length','min'=>3,'max'=>16,'message'=>'系统允许长度3-16位'),
            array('password','length','min'=>6,'max'=>16,'message'=>'密码最少6位,最长16位'),
            array('password2','compare','compareAttribute'=>'password','message'=>'两次密码不一致,请重输入'),
            array('email','email','message'=>'请输入一个有效E-mail'),
            //array('user','unique','className'=>'user','message'=>'用户已经存在'), //这个是不是检测字段的唯一性?
        );
}
	
}

最后提交的验证控制方法

public function actionCheck()
{
    $Model=new Reg();
    if(isset($_POST['Reg']))
    {
        $model->attributes=$_POST['Reg'];
        if($Model->validate())
        {
            //添加注册用户
            $admin=new Admin;
            $admin->attributes=$_POST['Reg'];
            $admin->save();
            echo "aa";
            //$this->redirect(Yii::app()->user->returnUrl);
        }else{
            echo 'b';
        }
    }
}
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册