Answer the question
In order to leave comments, you need to log in
Yii2 how to load array from gridview?
Tell me there is
<?php
$form = ActiveForm::begin(['id' => $model->formName()]
);
?>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'attribute' => 'id',
'label' => Yii::t('frontend', 'Id'),
],
[
'attribute' => 'username',
'label' => Yii::t('frontend', 'Name'),
],
['class' => 'yii\grid\CheckboxColumn',
'name' => 'broker_id',
'checkboxOptions' => function ($model, $key, $index, $column) {
return ['value' => $model->id];
}
],
],
]);
?>
<div class="form-group">
<?= Html::submitButton(Yii::t('common', 'Save'), ['class' => 'btn btn-info', 'name' => 'packages-button']) ?>
</div>
<?php
ActiveForm::end();
?>
public function rules() {
return [
,
];
}
public function actionIndex() {
$model = new BrokersForm();
$brokers = User::find()->where(['user_type' => 'broker']);
$dataProvider = new ActiveDataProvider([
'query' => $brokers,
'sort' => [
'defaultOrder' => ['id' => SORT_DESC],
], 'pagination' => [
'pageSize' => 10,
],
]);
var_dump($model->load(Yii::$app->request->post()));
// die();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Брокер выбран.');
return $this->refresh();
} else {
Yii::$app->session->setFlash('error', 'Ошибка брокер не выбран.');
return $this->refresh();
}
} else {
return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
}
Answer the question
In order to leave comments, you need to log in
Each instance of the class inherited from \yii\base\Model (including ActiveRecord) returns the name of the form, this name is first used to form inputs through ActiveField, and then to load data from these inputs (in the load method). In your case, CheckboxColumn forms inputs without taking into account the name of the form, and load tries to load data taking into account the name of the form. Therefore, your data is not loaded. The simplest solution is to do it like this:
But of course, wrapping the GridView in an ActiveForm is not very true.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question