Yii 2.0: Handling tabular data loading and validation in Yii 2


Problem Statement

How do you read and handle tabular data submission via form in Yii framework 2.0? Sometimes we want to collect user input in a batch mode. That is, the user can enter the information for multiple model instances and submit them all at once. We call this tabular input because the input fields are often presented in an HTML table.


If you are coming over from Yii 1 - the concepts of tabular data handling remain the same as mentioned in the Yii 1.x guide - collecting tabular input section.

The only differences in Yii 2 is that its much simpler due to available functions in the Model class for loading and validating models.


You can use a prebuilt solution to render and manage tabular data. You can use the TabularForm widget.


You can use the same concepts as mentioned in the guide link mentioned before - here are the changes you need to do for Yii 2:

To work with tabular input, we first need to create or populate an array of model instances, depending on whether we are inserting or updating the data. We then retrieve the user input data from the $_POST variable and assign it to each model. A slight difference from single model input is that we retrieve the input data using $_POST['ModelClass'][$i] instead of $_POST['ModelClass'].

Batch Update Controller Action

use yii\base\Model;
public function actionBatchUpdate()
    // retrieve items to be updated in a batch mode
    // assuming each item is of model class 'Item'
    if (Model::loadMultiple($items, Yii::$app->request->post()) && 
        Model::validateMultiple($items)) {
        $count = 0;
        foreach ($items as $item) {
           // populate and save records for each model
            if ($item->save()) {
                // do something here after saving
        Yii::$app->session->setFlash('success', "Processed {$count} records successfully.");
        return $this->redirect(['index']); // redirect to your next desired page
    } else {
        return $this->render('update', [
            'items' => $items,   

Your View File Sample

<div class="form">
    <?php $form = ActiveForm::begin(); ?>
        <?php foreach($items as $i=>$item): ?>
                <td><?= $form->field($item,"[$i]name"); ?></td>
                <td><?= $form->field($item,"[$i]price"); ?></td>
                <td><?= $form->field($item,"[$i]count"); ?></td>
                <td><?= $form->field($item,"[$i]description"); ?></td>
        <?php endforeach; ?>
    <?= Html::submitButton('Save'); ?>
    <?php ActiveForm::end(); ?>
</div><!-- form -->

Total 3 comments

#19121 report it
Veseliq at 2015/03/23 09:04am
When validation fails, errors are not rendered

Great tutorial. Although when validation for given field (e.g. name is required) fails, even after re-rendering, the errors are not shown.

<?= $form->field($item,"[$i]name"); ?>
#18936 report it
Felipe Marcellino at 2015/02/05 06:52pm
Inline validation for dynamic instances

How can I configure my code so that dynamic instances are validated inline, before the first submit? Does Yii offer some solution to it or must I create my own with AJAX and yiiActiveForm.js?

#18845 report it
Thani at 2015/01/21 07:38pm
Model File


Can you please give me example for implementing getItemsToUpdate() function.


Thanks, Thani.

Leave a comment

Please to leave your comment.

Write new article