Search code examples
phpyiiyii2

Send id chosen in dropdown list to action Yii2


I have a quick question. I implemented a dropdownList in a _form.php. Now my action crate won't work properly anymore. I am not sure if there is an issue with me sending the request to the action. But it's not really doing the trick anymore.

With the $form->field($model, 'team_idteam')->textInput() it worked just fine. So, this is what I have so far on the _form.php:

<div class="user-has-team-form">

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

    <?= $form->field($model, 'team_idteam')->textInput() //<-- This works perfectly. ?>

    <?= $form->field($model, 'teamIdteam')->dropDownList(ArrayHelper::map(Team::find()->all(), 'idteam', 'name')) <-- This does not work at all ?>

    <?= $form->field($model, 'user_iduser')->textInput() ?>

    <?= $form->field($model, 'oncallduty')->checkbox() ?>

    <div class="form-group">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

My actionCreate looks like this:

public function actionCreate()
    {
        $model = new UserHasTeam();

        if ($this->request->isPost) {
            Yii::info("Test1"); // <-- It get's up to this point.
            if ($model->load($this->request->post()) && $model->save()) {
            Yii::info("Test2");
                return $this->redirect(['view', 'team_idteam' => $model->team_idteam, 'user_iduser' => $model->user_iduser]);
            }
        } else {
            $model->loadDefaultValues();
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

The visuals work perfect and I can even chose different teams. If I create new teams, or delete old ones, they are shown or not shown as well. I have to admit I am a bit lost here.

EDIT I dumped the $_POST array after the $model = new UserHasTeam(); and it gave out the following array:

[
    '_csrf' => '0rkl0EAuFwjy9kdJNVQfVTQOkT22Kzo8bdvLAg2X0P_i0Ui-DEAkQ6WxfzsEAkwfBE_1UvNCDlsEjLtOefXmyA==',
    'UserHasTeam' => [
        'teamIdteam' => '3',
        'user_iduser' => '1',
        'oncallduty' => '0',
    ],
]

Solution

  • Yep. I am quite an idiot every now and then.

    This is how I solved it:

    <?= $form->field($model, 'team_idteam')->dropDownList(ArrayHelper::map(Team::find()->all(), 'idteam', 'name')) ?>