2017-07-31 22 views
0

在查看我聽說構建數據不是很好,但無論如何,我想知道爲什麼它不工作:的Yii 2下拉值安全新手

查看

<?php $form = ActiveForm::begin(); 

$alleSpieler = \common\models\Spieler::find()->all(); 

if ($alleSpieler) { 
unset($types); 
foreach ($alleSpieler as $value) { 
       $types[$value->id] = $value->email . ' ' . $value->vorname . ' ' . $value->nachname; 
           } 
         } 

echo $form->field($model, 'spielerId')->dropDownList($types, 'prompt'=>'Spieler manuell hinzufügen']); 

          ActiveForm::end(); 
          ?> 

<?= AnmeldungDurchfuehrung2::widget(['durchfuehrungId' => $model->id, 'spielerId' => $model->spielerId]) ?> 

型號

public $spielerId; 

但spielerId IST沒有我的情況下設置。例如,如果我在窗口小部件調用中設置'spielerId'=> 1120,它正在工作。但是,如果我想從下拉列表中獲取值,則操作是說spielerId丟失。我是新手,也許我忘了什麼?謝謝!

+0

我有什麼做$ spielerId充滿了從下拉列表中ID? –

+0

你需要爲下拉的改變事件寫入javascript –

+0

你能幫我用javascript嗎?是這樣的嗎? 回聲$形式 - >字段($模型, 'spielerId') - > DROPDOWNLIST($類型,陣列( '平變化'=> '的getData($模型 - > spielerId)', '類'=> '形式控制' ,'prompt'=>' - 選擇一個Player-')); –

回答

1

你必須在你的模型,索姆驗證陣列添加$spielerId;這樣的:

public function rules() 
{ 
    return [ 
     [['spielerId'], 'integer'], //type of atribute value 
     [['spielerId'], 'required'], //if need 
     /*... other atributes ...*/ 
    ]; 
} 

的詳細檢查documentation

+0

這個我已經在模型 –

+0

對不起,我錯誤地忽略了你的問題,在工作yii2的理解中做出了一些擺脫,首先你需要在模型中上傳數據來使用它們。在你的情況我recomendet創建創建單獨的操作,以在spielerId字段更改值後呈現小部件,並使用$ this-> renderAjax函數來返回它。 – vityapro

+1

因此,你需要: 1)JS腳本巫婆將蜂行動派選擇的值和處理響應(插入您的resived內容) 2)控制器女巫的行動將得到的數據和渲染視圖:'$這個 - > renderAjax(」 _sform '[ 'ID'=> $ ID, 'spielerId'=> $ spielerId]);' 3)考慮到與內容:'<= AnmeldungDurchfuehrung2 ::插件([? 'durchfuehrungId'=> $ ID,' spielerId '=> $ spielerId])?> – vityapro

0

還好現在我知道我需要什麼:

echo $form->field($model, 'spielerId')->dropDownList($types,['prompt'=>'Waehlen Sie einen Spieler']); 
echo Html::submitButton('Auswählen', ['class' => 'btn btn-primary']); 

這是我的下拉。

我需要這樣的東西:

<?php if(!empty($_GET['spielerId'])) { 
         echo AnmeldungDurchfuehrung2::widget(['durchfuehrungId' => $model->id, 'spielerId' => $_GET['subject']]); }?> 
+0

好吧現在我知道了,只需要設置字段的值名稱。 –