Changes
Title
unchanged
Handling tabular data loading and validation in Yii 2
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
yii, yii2, tabular, data, multiple, record, submission, validation, save
Content
changed
[...]
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](http://www.yiiframework.com/doc/guide/1.1/en/form.table "").
The only differences in Yii 2 is that its much simpler due to available functions in the Model class for loading and validating models.
### OPTION 1: USING A PREBUILT SOLUTION
You can use a prebuilt solution to render and manage tabular data. You can use the [TabularForm widget](http://demos.krajee.com/builder-details/tabular-form "") or [yii2-dynamicform](https://github.com/wbraganca/yii2-dynamicform).
### OPTION 2: DOING IT YOURSELF[...]
// is of model class 'Item'.
// Note the getItemsToUpdate method is an example method, where you
// fetch the valid models to update in your tabular form. You need
// to write such a method OR directly call your code to get the models.
$items=$this->getItemsToUpdate();
if (Model::loadMultiple($items, Yii::$app->request->post()) &&
Model::validateMultiple($items)) {
$count = 0;
foreach ($items as $item) {[...]