V
V
Vladimir2016-09-18 18:11:12
Yii
Vladimir, 2016-09-18 18:11:12

How to pass multiple values ​​in checkboxes in yii2?

Good day! There are several checkboxes, when I submit the form, only the value of the first button is entered into the database. The rest are lost...

public function actionAddHouse(){
        $model = new House();
        $realtors = Realtors::find()->asArray()->all();
        if($model->load(Yii::$app->request->post())){
            if($model->save()){
                Yii::$app->session->setFlash('success', 'Дом успешно поставлен на продажу!');
                return $this->refresh();
            } else {
                Yii::$app->session->setFlash('error', 'Ошибка заполнения формы!');
            }
        }
        return $this->render('house', compact('model', 'realtors'));
    }

And here is how the boxes themselves are implemented:
<?php $items = \yii\helpers\ArrayHelper::map($realtors, 'id', 'name') ?>
<?=$form->field($model, 'realtor_id')->checkboxList($items) ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-09-18
@MasterGerold

radio buttons

why then checkboxList?
If radio, then everything is correct, they should give 1 choice from the set, if it is still checkbox, then see what flies in the post request. And what do you end up saving? There will be a non-standard crud with checkboxes, since an array should fly away, which then apparently needs to either be driven into json or sorted out and saved where necessary.
Your action shouldn't save anything unless beforeSave is overridden in the model. If overridden, then its code in the studio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question