2015-10-13 28 views
0

我有一個表單,可以上傳他/她自己的簡歷文件(docx/pdf)。我有一個下拉菜單「員工姓名」,其中只顯示了登錄用戶名在列表中。但現在我想在輸入字段中顯示登錄用戶名。我可以這樣做嗎?可以任何一個給我代碼示例。因爲我在編碼方面很差。 這是我從代碼:如何在Yii1中創建時在輸入表單字段中顯示登錄用戶名?

 if($UserType == "employee") 
    { 
     $criteria=new CDbCriteria(); 
     $criteria->condition = "status= 'active' and id = $ID"; 
     echo $form->dropDownListGroup(
      $model, 
      'user_id', 
      array(
       'wrapperHtmlOptions' => array(
        'class' => 'col-sm-5', 
       ), 
       'widgetOptions' => array(
        'data' => CHtml::listData(User::model()->findAll($criteria), 'id', 'user_id'), 

        'htmlOptions' => array('prompt'=>'Select'), 
       ) 

      ) 
     ); 

     } 

user name input field logged-in user name

回答

0

這是我的方式做:

首先,在/保護/組件,我設置會話變量來存儲已登錄在用戶名中:

public function authenticate() 
{ 
    $user = Users::model()->find('user = ? ', array($this->username)); //user entered when trying to log in 
... 
} 

如果認證正確:

public function authenticate() 
{ 
... 
    $session = new CHttpSession; 
    $session->open(); //session_start 
    $session['user'] = $user; //$user is the logged-in username 
... 
} 

在控制器動作創建:

public function actionCreate(){ 
    $session = new CHttpSession; 
    $session->open(); 
    $user = $session['user']; 

    ... 

    $this->render('create',array(
    'user'=>$user, 
    'model'=>$model, 
    ... 
)); 
} 

鑑於(在/ _form爲例):

... 
$model->user = $user; 

$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id'=>'user-form', 
    'enableAjaxValidation'=>false, 
    'htmlOptions' => array('enctype' => 'multipart/form-data'), 
)); 
... 
echo $form->dropDownList($model,'user',...); //Here, the attribute 'user' of the model $model will have the logged-in user-name 
... 

這是假設你在你的模型中有一個名爲 '用戶' 屬性。如果沒有,那麼,在/模型,以便在你的/ _form使用此屬性創建一個輔助屬性「用戶」:

class YourModel extends CActiveRecord 
{ 
    public $user; //$variable used to set the logged-in username 
... 
} 
+0

感謝@Mundo你給我的解決方案,它是一個很大的幫助...... 。 – shopnil

相關問題