2013-01-14 69 views
0

我有一個CActiveForm,它在提交時應該使第二個CActiveForm可見。我知道如何在創建時更改表單的htmlOptions,但不知道如何通過控制器訪問它。Yii:提交表單以提交表單提交時,從控制器訪問CActiveForm.html選項

我的看法有兩種形式。第二種形式具有可見性:隱藏

<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)); 
} 

回答

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的任何方式。這就是爲什麼我提出這個解決方案。

+0

謝謝,這很好。 :) – TrueWheel