webyjh 2011-12-23 11:26:56 2822次浏览 1条回复 0 0 0

想在布局(比如column3.php)里放一个loginForm,通常应该怎么做?

  • 回复于 2011-12-23 11:44 举报

    我的做法是把控制器里的代码和视图里的代码全移到了视图中,效果是正确的,不知道是否规范?

    <div class="form">
    <?php 
    $form=$this->beginWidget('CActiveForm', array(
        'id'=>'login-form',
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),
    )); 
    ?>
    
    <?php
    $model=new LoginForm;
    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    
    // collect user input data
    if(isset($_POST['LoginForm'])){
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
    if($model->validate() && $model->login())
        $this->redirect(Yii::app()->user->returnUrl);
    }
    ?>
    <div class="row">
        <?php echo $form->labelEx($model,'用 户:'); ?>
        <?php echo $form->textField($model,'username',array('class'=>'text')); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>
    
    <div class="row">
        <?php echo $form->labelEx($model,'密 码: '); ?>
        <?php echo $form->passwordField($model,'password',array('class'=>'text')); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>
    
    <div class="row rememberMe">
        <?php echo $form->checkBox($model,'rememberMe'); ?>
        <?php echo $form->label($model,'rememberMe'); ?>
        <?php echo $form->error($model,'rememberMe'); ?>
    </div>
    
    <div class="row buttons">
        <?php echo CHtml::submitButton('登 录',array('class'=>'button')); ?>
        <?php echo CHtml::Button('注 册',array('class'=>'button')); ?>
    </div>
    
    <?php $this->endWidget(); ?>
    </div><!-- form -->
    
您需要登录后才可以回复。登录 | 立即注册