# DynamicForm

> Clone nested form rows in Yii2 ActiveForm

Package entry: `yii2-extensions/dynamicform`. Requires PHP `>=8.1`, Yii2 `^2.0.55 || ^22.0`. Checked 2026-11-11. [View the entry in the package directory](</docs/packages/yii2-extensions/dynamicform/index.md>)

**Clone nested form rows in Yii2 ActiveForm**

_Tabular inputs, client validation, nested widgets and jQuery events_

Add and remove rows of form fields in the browser while the tabular input names, ids and client validation stay
coherent. The widget wraps the rows rendered by `ActiveForm`, and the `Model` helper rebuilds the posted rows on the
server.

![Example application with nested property and room fields, live totals and custom styling](https://github.com/yii2-extensions/dynamicform/raw/main/images/dynamicform-light.png)

## Quick start

### Installation

```bash
composer require --prefer-dist yii2-extensions/dynamicform:^2.0
```

### Basic usage

Wrap the rows of a tabular form with `DynamicFormWidget::begin()` and `end()`:

```php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use Yii2\Extensions\DynamicForm\DynamicFormWidget;

$form = ActiveForm::begin(['id' => 'dynamic-form']);

DynamicFormWidget::begin(
    [
        'widgetContainer' => 'dynamicform_wrapper',
        'widgetBody' => '.container-items',
        'widgetItem' => '.item',
        'limit' => 4,
        'min' => 1,
        'insertButton' => '.add-item',
        'deleteButton' => '.remove-item',
        'model' => $modelsAddress[0],
        'formId' => 'dynamic-form',
        'formFields' => ['full_name', 'city'],
    ],
);
?>
<div class="container-items">
    <?php foreach ($modelsAddress as $i => $modelAddress): ?>
        <div class="item">
            <?= $form->field($modelAddress, "[{$i}]full_name")->textInput() ?>
            <?= $form->field($modelAddress, "[{$i}]city")->textInput() ?>
            <button type="button" class="add-item">+</button>
            <button type="button" class="remove-item">-</button>
        </div>
    <?php endforeach; ?>
</div>
<?php
DynamicFormWidget::end();

echo Html::submitButton('Save');

ActiveForm::end();
```

On the server, `Model::createMultiple()` builds one `Address` model per posted row; the
[usage examples](https://github.com/yii2-extensions/dynamicform/blob/main/docs/examples.md) show the complete create and
update actions.

## Documentation

- 📚 [Installation guide](https://github.com/yii2-extensions/dynamicform/blob/main/docs/installation.md)
- ⚙️ [Configuration guide](https://github.com/yii2-extensions/dynamicform/blob/main/docs/configuration.md)
- 💡 [Usage examples](https://github.com/yii2-extensions/dynamicform/blob/main/docs/examples.md)
- 🧪 [Testing guide](https://github.com/yii2-extensions/dynamicform/blob/main/docs/testing.md)

## AI assistance

AI agents: read the [documentation index](https://yii2-framework.github.io/docs/llms.txt) and fetch the Markdown version
of a page by appending `index.md` to its URL. The
[AI agents guide](https://yii2-framework.github.io/docs/project/ai-agents/) covers skill installation, client setup and
verification.

- 🤖 [`yii2-dynamicform` skill](https://github.com/yii2-extensions/dynamicform/blob/main/docs/agents.md):
  `DynamicFormWidget` rows, nesting, client validation, events and enhanced controls, and server-side rebuilding of
  posted rows with `Model::createMultiple()`.
- 🧭 [`yii2-framework` skill](https://yii2-framework.github.io/docs/guide/skills/yii2-framework/): Yii 22.0 application
  conventions.

The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the
[repository guides](#documentation), the [skill source](https://github.com/yii2-extensions/dynamicform/blob/main/skills/yii2-dynamicform/SKILL.md), the source PHPDoc and the
[changelog](https://github.com/yii2-extensions/dynamicform/blob/main/CHANGELOG.md).

## Special thanks

This extension was created by Wanderson Bragança and hosted at
[wbraganca/yii2-dynamicform](https://github.com/wbraganca/yii2-dynamicform). We thank him for his work; for continuity
and maintenance we have moved a copy here and preserve his identity throughout the code he wrote.

## Our social networks

[Follow on X](https://x.com/Terabytesoftw)
[Follow on Facebook](https://www.facebook.com/wilmer.arambula.9)
[Join our Subreddit](https://www.reddit.com/r/Yii2/)
[Join on Telegram](https://t.me/yii_framework_in_english)

## License

[License](https://github.com/yii2-extensions/dynamicform/blob/main/LICENSE.md)

## Package

- Source code and issues: [yii2-extensions/dynamicform](https://github.com/yii2-extensions/dynamicform). Star the repository if the package is useful to you.
- Composer package: [yii2-extensions/dynamicform](https://packagist.org/packages/yii2-extensions/dynamicform).
- Changelog: [CHANGELOG.md](https://github.com/yii2-extensions/dynamicform/blob/main/CHANGELOG.md).