DynamicFormWidget
API reference › DynamicForm › DynamicFormWidget
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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$deleteButton | string | Non-empty CSS selector of the buttons that delete a row. Default: null. | DynamicFormWidget |
$formFields | list<string>| | Fields to be validated. Default: null. | DynamicFormWidget |
$formId | string | Form ID Default: null. | DynamicFormWidget |
$id | string | Id of the widget. | yii\ |
$insertButton | string | Non-empty CSS selector of the buttons that insert a row. Default: null. | DynamicFormWidget |
$insertPosition | string | ‘bottom’ or ‘top’; Default: 'bottom'. | DynamicFormWidget |
$limit | int | Maximum row count for client-side insertion. Default: 999. | DynamicFormWidget |
$min | int | Minimum row count for client-side deletion. Default: 1. | DynamicFormWidget |
$model | yii\| | The model used for the form Default: null. | DynamicFormWidget |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
$widgetBody | string | CSS selector of the element that holds the rows. Default: null. | DynamicFormWidget |
$widgetContainer | string | Class of the container rendered around the rows; letters, digits and underscores only. Default: null. | DynamicFormWidget |
$widgetItem | string | CSS selector of one row; the first matching element supplies the client template. Default: null. | DynamicFormWidget |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the widget. | DynamicFormWidget |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
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\ |
renderFile() | Renders a view file. | yii\ |
run() | Renders the buffered rows and registers their template and client behavior. | DynamicFormWidget |
setId() | Sets the ID of the widget. | yii\ |
setView() | Sets the view object to be used by this widget. | yii\ |
trigger() | Triggers an event. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
getHashVarName() protected | Returns the hashed variable: the one registered for the form and container in the current page, if any. | DynamicFormWidget |
hashOptions() protected | Generates a hashed variable to store the options. | DynamicFormWidget |
initOptions() protected | Initializes the widget options. | DynamicFormWidget |
registerOptions() protected | Registers plugin options by storing it in a hashed javascript variable. | DynamicFormWidget |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
WIDGET_NAME | 'dynamicform' | DynamicFormWidget |
Method details
Section titled “Method details”registerAssets()
Section titled “registerAssets() ”public function registerAssets(yii\web\View $view): voidRegisters 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
| Parameter | Type | Description |
|---|---|---|
$view | yii\ | The View object |
registerHashVarWidget()
Section titled “registerHashVarWidget() ”public function registerHashVarWidget(): boolRegisters 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.
run()
Section titled “run() ”public function run(): mixedRenders the buffered rows and registers their template and client behavior.
Returns
mixed
getHashVarName()
Section titled “getHashVarName() ”protected function getHashVarName(): stringReturns the hashed variable: the one registered for the form and container in the current page, if any.
Returns
string
hashOptions()
Section titled “hashOptions() ”protected function hashOptions(): voidGenerates a hashed variable to store the options.
initOptions()
Section titled “initOptions() ”protected function initOptions(): voidInitializes the widget options.
registerOptions()
Section titled “registerOptions() ”protected function registerOptions(yii\web\View $view): voidRegisters plugin options by storing it in a hashed javascript variable.
Parameters
| Parameter | Type | Description |
|---|---|---|
$view | yii\ | The View object |