Skip to content
PHP Ecosystem
Explore

DynamicFormWidget

API reference › DynamicForm › DynamicFormWidget

classYii2\Extensions\DynamicForm\DynamicFormWidget
Extendsyii\base\Widget » yii\base\Component » yii\base\BaseObject
Implementsyii\base\ViewContextInterface, yii\base\Configurable
Sourcesrc/DynamicFormWidget.php

Yii2 Dynamic form widget for cloning form elements in a nested manner while maintaining accessibility.

A widget is registered once per page, form and container: its options, click handlers and initialization are declared by the first rendering, and later renderings with the same formId and widgetContainer in the same page, such as a nested widget rendered once per outer row, reuse that registration. They must serialize the same options except the row template; a different configuration under the same container of the same form is rejected. The page ends when the view is cleared, as View::endPage() and View::renderAjax() do.

PropertyTypeDescriptionDefined by
$autoIdPrefix staticstringThe prefix to the automatically generated widget IDs. Default: 'w'.yii\base\Widget
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$deleteButtonstringNon-empty CSS selector of the buttons that delete a row. Default: null.DynamicFormWidget
$formFieldslist<string>|nullFields to be validated. Default: null.DynamicFormWidget
$formIdstringForm ID Default: null.DynamicFormWidget
$idstringId of the widget.yii\base\Widget
$insertButtonstringNon-empty CSS selector of the buttons that insert a row. Default: null.DynamicFormWidget
$insertPositionstring‘bottom’ or ‘top’; Default: 'bottom'.DynamicFormWidget
$limitintMaximum row count for client-side insertion. Default: 999.DynamicFormWidget
$minintMinimum row count for client-side deletion. Default: 1.DynamicFormWidget
$modelyii\base\Model|nullThe model used for the form Default: null.DynamicFormWidget
$viewyii\web\ViewThe view object that can be used to render views or view files.yii\base\Widget
$viewPath read-onlystringThe directory containing the view files for this widget.yii\base\Widget
$widgetBodystringCSS selector of the element that holds the rows. Default: null.DynamicFormWidget
$widgetContainerstringClass of the container rendered around the rows; letters, digits and underscores only. Default: null.DynamicFormWidget
$widgetItemstringCSS selector of one row; the first matching element supplies the client template. Default: null.DynamicFormWidget
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
afterRun()This method is invoked right after a widget is executed.yii\base\Widget
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
beforeRun()This method is invoked right before the widget is executed.yii\base\Widget
begin() staticBegins a widget.yii\base\Widget
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
end() staticEnds a widget.yii\base\Widget
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getId()Returns the ID of the widget.yii\base\Widget
getView()Returns the view object that can be used to render views or view files.yii\base\Widget
getViewPath()Returns the directory containing the view files for this widget.yii\base\Widget
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the widget.DynamicFormWidget
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
registerAssets()Registers the needed assets.DynamicFormWidget
registerHashVarWidget()Registers the widget for its form and container in the current page of the view.DynamicFormWidget
render()Renders a view.yii\base\Widget
renderFile()Renders a view file.yii\base\Widget
run()Renders the buffered rows and registers their template and client behavior.DynamicFormWidget
setId()Sets the ID of the widget.yii\base\Widget
setView()Sets the view object to be used by this widget.yii\base\Widget
trigger()Triggers an event.yii\base\Component
widget() staticCreates a widget instance and runs it.yii\base\Widget
getHashVarName() protectedReturns the hashed variable: the one registered for the form and container in the current page, if any.DynamicFormWidget
hashOptions() protectedGenerates a hashed variable to store the options.DynamicFormWidget
initOptions() protectedInitializes the widget options.DynamicFormWidget
registerOptions() protectedRegisters plugin options by storing it in a hashed javascript variable.DynamicFormWidget
EventTypeDescriptionDefined by
EVENT_INITyii\base\EventAn event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11.yii\base\Widget
EVENT_BEFORE_RUNyii\base\WidgetEventAn event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11.yii\base\Widget
EVENT_AFTER_RUNyii\base\WidgetEventAn event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11.yii\base\Widget
ConstantValueDescriptionDefined by
WIDGET_NAME'dynamicform'DynamicFormWidget
public function init(): void

Initializes the widget.

public function registerAssets(yii\web\View $view): void

Registers the needed assets.

The click handlers are delegated from the form and act only when the clicked button belongs directly to a container of this registration, so widgets that share button selectors, such as nested widgets, never act on each other’s rows. beforeInsert is triggered on that container with the button; it is informational and cannot cancel the insertion.

Parameters

ParameterTypeDescription
$viewyii\web\ViewThe View object
public function registerHashVarWidget(): bool

Registers the widget for its form and container in the current page of the view.

Call it after hashOptions(). The registration stays in effect while the page holds the options declared by registerOptions().

Returns

bool: true when the widget was registered, false when the page already holds a compatible registration.

Throws

  • yii\base\InvalidConfigException: When the page already holds a registration for the same form and container whose options, other than the row template, differ.
public function run(): mixed

Renders the buffered rows and registers their template and client behavior.

Returns

mixed

protected function getHashVarName(): string

Returns the hashed variable: the one registered for the form and container in the current page, if any.

Returns

string

protected function hashOptions(): void

Generates a hashed variable to store the options.

protected function initOptions(): void

Initializes the widget options.

protected function registerOptions(yii\web\View $view): void

Registers plugin options by storing it in a hashed javascript variable.

Parameters

ParameterTypeDescription
$viewyii\web\ViewThe View object