Form component
Overview
The Form component is a collection of fields that can be grouped in tabs and fieldsets. It enables CRUD operations.
Form is a basic component.
Structure
Javascript constructor: form.js
Related UI components
The following components can be used in the scope of the Form component:
- ActionDelete
- Checkbox
- Checkboxset
- DataSource
- FieldSet
- FileUploader
- Hidden
- Input
- Multiline
- Multiselect
- Radioset
- Select
- Text
- Textarea
- Wysiwyg
Component options
Form configuration extends the uiCollection
configuration.
Form-specific configuration:
Option | Description | Type | Default |
---|---|---|---|
ajaxSave
|
Save Form values by AJAX. | Boolean |
false
|
ajaxSaveType
|
There are two possible approaches to collect form data for ajaxSave:
|
default |simple
|
default
|
component
|
The path to the component’s JS constructor in terms of RequireJS. | String |
Magento_Ui/js/form/form
|
exports
|
Used to notify some external entity about property
changing. exports value is an object, composed of the
following:
|
Object
|
|
imports
|
Used for tracking changes of an external entity property.
imports ’s value is an object, composed of the following:
|
Object
|
'${ $.provider}:reloadUrl'
|
messagesClass
|
The CSS class assigned to the <div> element, where the form elements validation error is rendered. |
String |
'messages'
|
selectorPrefix
|
The name that can be used to address the block to which this
attribute is assigned. The name must be unique per
generated page. If not specified, the name is
assigned automatically in the following format: ANONYMOUS_n
|
String |
'.page-content'
|
template
|
The path to the component’s .html template.
|
String |
'ui/form/field'
|
Create an instance of the Form component
To create an instance of the Form component, you need to do the following:
- In you custom module, add a configuration file for the instance, for example:
customer_form.xml
. - Add a set of fields (the Fieldset component with the component of the Field) for entity or to implement the upload of meta info in the DataProvider.
- Create the DataProvider class for the entity that implements DataProviderInterface
- Add a component in Magento layout as a node:
<uiComponent name="customer_form"/>
- Add a component in Magento layout as a node:
Example:
Configure the Form component
Component could be configured in two ways:
- globally: using any module’s
view/ui_component/etc/definition.xml
file. All settings declared in this file will be applied to all component’s instances - locally: using concrete component instance configuration, such as
<your module root dir>/Magento/Customer/view/base/ui_component/customer_form
Create configuration file: <your module root dir>/Magento/Customer/view/base/ui_component/customer_form.xml
Nodes are optional and contain parameters required for component:
-
js_config -> deps - sets the dependency on component initialization
-
js_config -> config -> provider - specifies the name of the component data
-
layout - configuration class meets the visualization component. Names for deps and provider are specified with a complete path from the root component with the separator “.”
Add a description of the fields in the form using components and Field Fieldset:
To group components you can use the component container as in example below:
Creating DataSource
You need to configure component’s DataSource in order to provide data and meta information for your Form component.
DataSource aggregates an object of class implements the interface \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface
An example of the configuration of the DataSource object:
Component configuration:
-
argument “dataProvider” - contains configuration, class name and arguments
-
js_config -> component - > JS indication of a responsible component
Data provided by data source is shared and available for all components in the Assembly (in this case for all child components of UI Form).
Data Source is another UI Component that provides data in specific format which is shared among all UI Components.
Replacing
Replacing principles are the same for all UI Components.
Global replacement
To replace all instances of a UI Form with a custom implementation redefine link to a constructor in definition.xml
.
app/code/Magento/Ui/view/base/ui_component/etc/definition.xml
Instance Replacement
To replace one instance of a UI Form Component redefine link to a constructor in your module’s form configuration file:
app/code/Magento/Customer/view/base/ui_component/customer_form.xml