T
T
teodor7teodor72017-01-05 12:40:35
Yii
teodor7teodor7, 2017-01-05 12:40:35

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();
?>

You need to accept a lot of check boxes and process. As far as I understand there are 2 ways.
1) This is validation in rules
public function rules() {
        return [
                ,
        ];
    }

2) Another way is Model::validateMultiple([broker_id])
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]);
        }

But none of them work, I don't understand why.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-01-05
@qonand

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 question

Ask a Question

731 491 924 answers to any question