0

我有一个 CActiveForm 提交时应该使第二个 CActiveForm 变得可见。我知道如何在创建表单时更改表单的 htmlOptions,但不知道如何通过控制器访问它。

我的观点有两种形式。第二种形式有可见性:隐藏

<div class="form">
            <?php
            $numberForm = $this->beginWidget('CActiveForm', array(
                'id' => 'addnumber-form',
                'enableAjaxValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                    ));
            ?>

            <p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>
            <?php echo $numberForm->errorSummary($numberModel); ?>

            <div class="row">
                <?php echo $numberForm->labelEx($numberModel, 'number'); ?>
                <?php echo $numberForm->textField($numberModel, 'number'); ?>
                <?php echo $numberForm->error($numberModel, 'number'); ?>

                <?php
                echo CHtml::submitButton(UserModule::t("Verify"), array(
                    "class" => "btn btn-success"
                ));
                ?>

            </div>

            <?php $this->endWidget(); ?>

            <?php
            $verifyForm = $this->beginWidget('CActiveForm', array(
                    'id' => 'verify-form',
                    'enableAjaxValidation' => true,
                    'clientOptions' => array(
                        'validateOnSubmit' => true,
                    ),
                    'htmlOptions' => array("style"=>"visibility: hidden"),
                ));
            ?>

            <?php echo $verifyForm->errorSummary($verifyModel); ?>

            <p>A authorisation code has been sent to your phone. Please enter it below. If you don't receive a text message make sure you entered your number correctly and try again</p>

            <div class="row">
                <?php echo $verifyForm->labelEx($verifyModel, 'authcodeUser'); ?>
                <?php echo $verifyForm->textField($verifyModel, 'authcodeUser'); ?>
                <?php echo $verifyForm->error($verifyModel, 'authcodeUser'); ?>

                <?php
                echo CHtml::submitButton(UserModule::t("Confirm"), array(
                    "class" => "btn btn-success"
                ));
                ?>

                <?php
                    foreach(Yii::app()->user->getFlashes() as $key => $message) {
                        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
                    }
                ?>

            </div>

            <?php $this->endWidget(); ?>


        </div><!-- form --> 

我的这些表格的控制器

public function actionAddnumber(){
    $numberModel = new UserAddNumber;
    $verifyModel = new UserVerifyNumber;
    if (Yii::app()->user->id) {

        // ajax validator
        if(isset($_POST['ajax']) && $_POST['ajax']==='addnumber-form')
        {
            echo UActiveForm::validate($numberModel);
            Yii::app()->end();
        }

        if(isset($_POST['UserAddNumber'])) {
                $numberModel->attributes=$_POST['UserAddNumber'];
                if($numberModel->validate()) {
                    $profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
                    $profile->mobileNo = $numberModel->number;
                    $profile->save();

                    //MAKE $verifyForm visibility to visible uring htmlOptions

                    Yii::app()->session['authcode'] = '4444';
                }
        }

        if(isset($_POST['UserVerifyNumber'])) {
                $verifyModel->attributes=$_POST['UserVerifyNumber'];
                if($verifyModel->validate()) {
                    $profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
                    $profile->mobileNoVerified = True;
                    $profile->save();
                    Yii::app()->user->setFlash('profileMessage',UserModule::t("Your mobile number has been verified"));
                    $this->redirect(array("profile"));

                }
        }

    }
    $this->render('addnumber', array('numberModel'=>$numberModel, 'verifyModel' => $verifyModel));
}
4

1 回答 1

2

看起来您可以创建一个新变量来确定是否显示第二种形式,然后将其传递给视图。这是您的控制器:

public function actionAddnumber(){
    $numberModel = new UserAddNumber;
    $verifyModel = new UserVerifyNumber;
    $formVisibility = "hidden";

    if (Yii::app()->user->id) {

        // ajax validator
        if(isset($_POST['ajax']) && $_POST['ajax']==='addnumber-form')
        {
            echo UActiveForm::validate($numberModel);
            Yii::app()->end();
        }

        if(isset($_POST['UserAddNumber'])) {
                $numberModel->attributes=$_POST['UserAddNumber'];
                if($numberModel->validate()) {
                    $profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
                    $profile->mobileNo = $numberModel->number;
                    $profile->save();

                    //MAKE $verifyForm visibility to visible uring htmlOptions
                    $formVisibility = "visible";


                    Yii::app()->session['authcode'] = '4444';
                }
        }

        if(isset($_POST['UserVerifyNumber'])) {
                $verifyModel->attributes=$_POST['UserVerifyNumber'];
                if($verifyModel->validate()) {
                    $profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
                    $profile->mobileNoVerified = True;
                    $profile->save();
                    Yii::app()->user->setFlash('profileMessage',UserModule::t("Your mobile number has been verified"));
                    $this->redirect(array("profile"));

                }
        }

    }
    $this->render('addnumber', array('numberModel'=>$numberModel, 'verifyModel' => $verifyModel, 'formVisibility' => $formVisibility));
}

这是您的第二种形式的第一部分:

<?php
            $verifyForm = $this->beginWidget('CActiveForm', array(
                    'id' => 'verify-form',
                    'enableAjaxValidation' => true,
                    'clientOptions' => array(
                        'validateOnSubmit' => true,
                    ),
                    'htmlOptions' => array("style"=>"visibility: ".$formVisibility),
                ));
            ?>

[编辑] 为确保我回答您的问题,我应该补充一点,我从未见过任何直接从控制器更改 htmlOptions 的方法。这就是我提出这个解决方案的原因。

于 2013-01-14T19:16:55.680 回答