Dynamic forms¶
Element supports dynamic forms using Angular Formly which allows to generate forms from JSON data.
Code¶
Usage¶
Required Packages
import { SiFormlyModule } from '@siemens/element-ng/formly';
@NgModule({
imports: [SiFormlyModule, ...]
})
Standalone support¶
Formly does not provide standalone support yet see also.
However, starting from Element v46.5.0 you can use the workaround provided in comment.
Include SiFormlyModule in your app configuration to register your custom types/validators/wrappers.
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { SiFormlyModule } from '@siemens/element-ng/formly';
const config: ApplicationConfig = {
providers: [
importProvidersFrom(
SiFormlyModule.forRoot({
// 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.
checkboxbuttonUse
btnTypeproperty with props in field config to configure following supported button types:- primary
- secondary
- tertiary
- danger
- warning
booleandateThe 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
emailenumintegernumberThe 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
stringtextdisplay- 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
textareatimeThe 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-formlyInput 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<(undefined | FormlyForm)> | ... | 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.