Перейти до вмісту
PHP Ecosystem
Explore

DynamicForm

Цей контент ще не доступний вашою мовою.

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

Terminal window
composer require --prefer-dist yii2-extensions/dynamicform:^2.0

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

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 show the complete create and update actions.

AI agents: read the documentation index and fetch the Markdown version of a page by appending index.md to its URL. The AI agents guide covers skill installation, client setup and verification.

  • 🤖 yii2-dynamicform skill: DynamicFormWidget rows, nesting, client validation, events and enhanced controls, and server-side rebuilding of posted rows with Model::createMultiple().
  • 🧭 yii2-framework skill: 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, the skill source, the source PHPDoc and the changelog.

This extension was created by Wanderson Bragança and hosted at 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.

Follow on X Follow on Facebook Join our Subreddit Join on Telegram

License