Dynamic forms¶
Element supports dynamic forms using Angular Formly which allows to generate forms from JSON data.
Formly version
The @siemens/element-ng/formly package is based on Angular Formly v7.1.
If you are still using Formly v6, use @siemens/element-ng/formly-legacy instead.
Migrate from formly-legacy
@siemens/element-ng/formly-legacy (Formly v6) is deprecated and support
will be dropped in a future release. Migrate to @siemens/element-ng/formly
(Formly v7.1) as soon as possible.
Code¶
Usage¶
Required Packages
import { SiFormlyComponent } from '@siemens/element-ng/formly';
@Component({
imports: [SiFormlyComponent, ...]
})
Standalone support¶
SiFormlyComponent is a standalone component. Import it directly into your
component's imports and register any custom types/validators/wrappers via
provideFormlyCore in your app configuration.
import { ApplicationConfig } from '@angular/core';
import { provideFormlyCore } from '@ngx-formly/core';
const config: ApplicationConfig = {
providers: [
provideFormlyCore({
// optionally you can add following configurations
types: [
// register your custom types here
],
validators: [
// register your custom validators here
],
wrappers: [
// register your custom wrapper here
]
})
]
}
SiFormlyComponent¶
The SiFormlyComponent is a wrapper around formly to generate dynamic forms.
Please note that if you don't use this wrapper your form items will not be themed correctly based on Element theme and you might have to manually manage theming which is not recommended.
Utilities¶
There are plenty of utilities custom-made for you to develop apps with dynamic forms using SiFormly.
Custom Types¶
In addition to prebuilt types from bootstrap, Element also provides you with a set of custom types that help you to easily make use of Element components in formly. The below types helps you to develop your dynamic forms app more easily.
Structural types¶
These types can be used to achieve a specific layout or wrapper for your form elements.
object-grid- Wraps your fields in a configurable bootstrap grid.
- Use
gridConfigproperty with props in field config to configure your grid.
array- Wraps your form array together with add/remove buttons if needed.
- Expects a field array type to be provided within the fields input.
accordion- Wraps your input fields in an accordion
tabset- Wraps your input fields in a tabset
Field types¶
In addition to the formly bootstrap types, the following custom types can be used to enable use of Element form components with formly.
checkbox-
buttonUse
btnTypeproperty with props in field config to configure following supported button types:- primary
- secondary
- tertiary
- danger
- warning
-
boolean -
dateThe following inputs can be passed as field config props:
- autoClose
- dateConfig
-
date-rangeThe following inputs can be passed as field config props:
- ariaLabelCalendarButton
- autoClose
- debounceTime
- endDatePlaceholder
- endTimeLabel
- siDatepickerConfig
- startDatePlaceholder
- startTimeLabel
-
datetimeThe following inputs can be passed as field config props:
- autoClose
- dateConfig
-
email enuminteger-
numberThe following inputs can be passed as field config props:
- numberStep
- showButtons
- unit
-
passwordThe following inputs can be passed as field config props:
- digits
- lowerCase
- minLength
- special
- upperCase
-
si-select- use
multiproperty with props in field config to enable multi selection mode.
- use
-
string -
textdisplay- use
prefixproperty with props in field config to add prefix before text. - use
suffixproperty with props in field config to add suffix after text.
- use
-
textarea -
timeThe following inputs can be passed as field config props:
- hideLabels
- showMeridian
- showMilliseconds
- showMinutes
- showSeconds
Custom wrappers¶
Use following wrappers to wrap your form fields with specific Element components.
form-field- uses
SiFormItemComponentto wrap your form fields
- uses
form-fieldset- uses
SiFormFieldsetComponent
- uses
form-field-horizontal- wraps your form field and label in a horizontal layout.
icon-wrapper- wraps your form field with an icon displayed at the end.
- The following inputs can be passed as field config props:
icon- the icon to be displayed at the end of the form field.
iconSize- the size of the icon to be displayed in pixels.
iconTooltip- Tooltip for the icon to be displayed.
Grid layout¶
SiFormly also supports rendering form with multi column grid layout.
Tabs¶
SiFormly also supports rendering form in multiple tabs. It uses SiTabsetComponent internally for displaying tabs.
Accordion¶
SiFormly also supports rendering forms in accordions. It makes use of SiAccordionComponent internally.
i18n support¶
SiFormly internally uses a custom extension translate to internationalize your forms.
It is configured similar to this doc on formly but without the hard dependency to ngx-translate library.
SiFormlyComponent API Documentation¶
si-formly
Input Properties¶
| Name | Type | Default | Description |
|---|---|---|---|
| fields ¶ |
FormlyFieldConfig<(FormlyFieldProps & { [ additionalProperties: string ]: any } })>[] |
[] |
Define all form fields with FormlyFieldConfig array. |
| form ¶ |
FormGroup<TControl> |
||
| labelWidth ¶ |
number |
Define width for field labels in pixel | |
| model ¶ |
{} | {} |
Mapping of field name and its value. |
| options ¶ |
FormlyFormOptions |
{} |
Define FormlyFormOptions. |
| schema ¶ |
JSONSchema7 |
JSONSchema7 can be used instead of FormlyFieldConfig array for defining form fields. |
Output Properties¶
| Name | Type | Description |
|---|---|---|
| fieldsChange ¶ |
FormlyFieldConfig<(FormlyFieldProps & { [ additionalProperties: string ]: any } })>[] |
|
| formChange ¶ |
FormGroup<TControl> |
|
| modelChange ¶ |
{} | Mapping of field name and its value. |
| schemaChange ¶ |
JSONSchema7 |
JSONSchema7 can be used instead of FormlyFieldConfig array for defining form fields. |
Attributes and Methods¶
| Name | Type | Default | Description |
|---|---|---|---|
| (readonly) formlyForm ¶ |
Signal<(FormlyForm | undefined)> |
... | Formly main container to provide modelChange subscriptions. |
Types Documentation¶
|
import
imported from @ngx-formly/core
|
|---|
|
import
imported from @ngx-formly/core
|
|---|
|
import
imported from @ngx-formly/core
|
|---|
|
import
imported from @types/json-schema
|
|---|
|
import
imported from @ngx-formly/core
|
|---|
Except where otherwise noted, content on this site is licensed under MIT License.