# ng2-dynamic-forms **Repository Path**: mirrors_un33k/ng2-dynamic-forms ## Basic Information - **Project Name**: ng2-dynamic-forms - **Description**: Rapid form development library for Angular 2.x and higher - **Primary Language**: Unknown - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-26 - **Last Updated**: 2026-11-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Logo ng2 Dynamic Forms # ng2 Dynamic Forms [![npm version](https://badge.fury.io/js/%40ng2-dynamic-forms%2Fcore.svg)](https://badge.fury.io/js/%40ng2-dynamic-forms%2Fcore) [![Build Status](https://travis-ci.org/udos86/ng2-dynamic-forms.svg?branch=master)](https://travis-ci.org/udos86/ng2-dynamic-forms) [![Coverage Status](https://coveralls.io/repos/github/udos86/ng2-dynamic-forms/badge.svg)](https://coveralls.io/github/udos86/ng2-dynamic-forms) ng2 Dynamic Forms is a **rapid form development library** based on the official Angular 2 [**dynamic forms cookbook**](https://angular.io/docs/ts/latest/cookbook/dynamic-form.html). It simplifies the time-consuming work of implementing reactive Angular 2 forms by building upon a layer of maintainable form control models to **fully automate form UI creation**. Therefore it provides a set of **dynamic UI components** with out of the box support for **[Bootstrap](http://getbootstrap.com)**, **[Foundation](http://foundation.zurb.com/)**, **[Material 2](https://github.com/angular/material2)** and [more](#ui-modules-and-components). See what's possible by exploring the [**live demo**](http://ng2-dynamic-forms.udos86.de/example/index.aot.html) and the [**API documentation**](http://ng2-dynamic-forms.udos86.de/docs/)! ## Table of Contents - [Getting Started](#getting-started) - [Running the Example](#running-the-example) - [Basic Usage](#basic-usage) - [UI Modules and Components](#ui-modules-and-components) - [Model Bindings and Control References](#model-bindings-and-control-references) - [Form Groups](#form-groups) - [Form Arrays](#form-arrays) - [Form Layouts](#form-layouts) - [Custom Templates](#custom-templates) - [Custom Validators](#custom-validators) - [Validation Messaging](#validation-messaging) - [JSON Export and Import](#json-export-and-import) - [Disabling and Enabling Form Controls](#disabling-and-enabling-form-controls) - [Related Form Controls](#related-form-controls) - [Autocomplete](#autocomplete) - [A Word to the Community](#a-word-to-the-community) - [Appendix](#appendix) ## Getting Started **1. Install the core package**: ``` npm install @ng2-dynamic-forms/core --save ``` **2. Choose your UI library** (e.g. [**Bootstrap**](http://getbootstrap.com)) and **install the appropriate package**: ``` npm install @ng2-dynamic-forms/ui-bootstrap --save ``` **3.** When using **SystemJS**, update your configuration to **import the corresponding UMD bundles**: ```ts System.config({ paths: { "npm:": "node_modules/" }, map: { // ...all the rest (Angular 2, RxJS, etc.) "@ng2-dynamic-forms/core": "npm:@ng2-dynamic-forms/core/bundles/core.umd.js", "@ng2-dynamic-forms/ui-bootstrap": "npm:@ng2-dynamic-forms/ui-bootstrap/bundles/ui-bootstrap.umd.js", } }); ``` ## Running the Example **1. Clone the Git repository**: ``` git clone https://github.com/udos86/ng2-dynamic-forms.git ``` **2. Install the npm dependencies**: ``` npm install ``` **3. Transpile the TypeScript files**: ``` npm run tsc ``` **4. Run the sample application**: ``` npm start ``` ## Basic Usage **1. Import the ng2 Dynamic Forms core** `NgModule` **and a corresponding UI** `NgModule`: ```ts import { NgModule } from "@angular/core"; import { BrowserModule } from "@angular/platform-browser"; import { ReactiveFormsModule } from "@angular/forms"; import { DynamicFormsCoreModule } from "@ng2-dynamic-forms/core"; import { DynamicFormsBootstrapUIModule } from "@ng2-dynamic-forms/ui-bootstrap"; // ..all remaining component and routing imports @NgModule({ imports: [ DynamicFormsCoreModule.forRoot(), DynamicFormsBootstrapUIModule, BrowserModule, ReactiveFormsModule // ...all remaining imports ], declarations: [AppComponent, MyDynamicFormComponent], bootstrap: [AppComponent] }) export class AppModule {} ``` **2. Define your dynamic form model**: ```ts import { DynamicFormControlModel, DynamicCheckboxModel, DynamicInputModel, DynamicRadioGroupModel } from "@ng2-dynamic-forms/core"; export const MY_DYNAMIC_FORM_MODEL: DynamicFormControlModel[] = [ new DynamicInputModel({ id: "exampleInput", label: "Example Input", maxLength: 42, placeholder: "example input" }), new DynamicRadioGroupModel({ id: "exampleRadioGroup", label: "Example Radio Group", options: [ { label: "Option 1", value: "option-1", }, { label: "Option 2", value: "option-2" }, { label: "Option 3", value: "option-3" } ], value: "option-3" }), new DynamicCheckboxModel({ id: "exampleCheckbox", label: "I do agree" }) ]; ``` **3. Create a** `FormGroup` **via** `DynamicFormService`: ```ts import { MY_DYNAMIC_FORM_MODEL } from "./my-dynamic-form.model"; import { DynamicFormControlModel, DynamicFormService } from "@ng2-dynamic-forms/core"; export class MyDynamicFormComponent implements OnInit { formModel: DynamicFormControlModel[] = MY_DYNAMIC_FORM_MODEL; formGroup: FormGroup; constructor(private formService: DynamicFormService) {} ngOnInit() { this.formGroup = this.formService.createFormGroup(this.formModel); } } ``` **4. Add the** `DynamicFormControlComponent` **to your template and bind it's** `FormGroup` **and** `DynamicFormControlModel`: ```ts
``` ## UI Modules and Components ng2 Dynamic Forms is built to provide **solid yet unobtrusive** support for a variety of common UI libraries: * **Basic** (pure HTML5) * **[Bootstrap](http://getbootstrap.com)** * **[Foundation](http://foundation.zurb.com)** * **[Kendo UI](http://www.telerik.com/kendo-angular-ui)** * **[Material 2](https://github.com/angular/material2)** * **[PrimeNG](http://www.primefaces.org/primeng/#/)** You can instantly plug in your favorite form controls by **installing the appropriate package and it's peer dependencies**: ``` npm install @ng2-dynamic-forms/ui- --save ``` Right afterwards **just import the corresponding UI** `NgModule`: ```ts @NgModule({ imports: [ DynamicFormsCoreModule.forRoot(), DynamicFormsBootstrapUIModule, BrowserModule, ReactiveFormsModule ], // ...all remaining definitions }) export class AppModule {} ``` Every UI `NgModule` declares a `DynamicFormControlComponent` that **can easily be added to your component** `template`: ```ts
``` **Also don't forget to refer the library stylesheet**: ```ts ``` Due to technical restrictions or external dependencies still being in development full support for all major form controls cannot be provided in every single UI library. **See the following compatibility table**: | | Checkbox | Datepicker | File Upload | Input | Radio Group | Select | Slider | Switch | Textarea | |:-------------: |:--------: |:----------: |:-----------: |:-----: |:-----------: |:------: |:------: |:------: |:--------: | | ui-basic | ✓ | * | ** | ✓ | ✓ | ✓ | *** | ✗ | ✓ | | ui-bootstrap | ✓ | * | ** | ✓ | ✓ | ✓ | *** | ✗ | ✓ | | ui-foundation | ✓ | * | ** | ✓ | ✓ | ✓ | *** | ✓ | ✓ | | ui-kendo | ✗ | ✓ | ✓ | ✗ | ✗ | ✗ | ✓ | ✓ | ✗ | | ui-material | ✓ | * | ** | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ui-primeng | ✓ | ✓ | ** | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | **\*)** date controls can be achieved using a `DynamicInputModel` with `inputType: "date"` or `inputType: "time"` **\*\*)** file upload controls can be achieved using a `DynamicInputModel` with `inputType: "file"` **\*\*\*)** slider controls can be achieved using a `DynamicInputModel` with `inputType: "range"` ## Model Bindings and Control References One of the benefits of using ng2 Dynamic Forms is that interacting with your form programmatically becomes pretty easy. Since a `DynamicFormControlModel` is bound directly to a `DOM` element via Angular 2 core mechanisms, changing one of it's properties will immediately trigger a UI update. Well, almost... ng2 Dynamic Forms relies on the Angular 2 `ReactiveFormsModule`. Therefore the `value` property **is not** two-way-bound via `[(ngModel)]` under the hood. *So what if we actually want to update the value of an arbitrary form control at runtime?* At first we need to get a reference to it's `DynamicFormControlModel` representation. This can easily be achieved either by a simple index-based array lookup or through the `findById` method of `DynamicFormService`: ```ts this.inputModel = this.formModel[2]; ``` ```ts this.inputModel = this.formService.findById("myInput", this.formModel) as DynamicInputModel; ``` We now have access to the `valueUpdates` `Rx.Subject` to push new values via `next()` as well as to listen to new user input via `subscribe()`: ```ts this.inputModel.valueUpdates.next("my new value"); this.inputModel.valueUpdates.subscribe(value => console.log("new value: ", value); ``` At any time we can also safely read the most recent user input from the `value` property: ```ts let currentValue = this.inputModel.value; ``` ## Form Groups In order to improve clarity it's often considered good practice to group forms into several logical `fieldset` sections. Luckily ng2 Dynamic Forms supports nesting of form groups out of the box! **1. Just create a** `DynamicFormGroupModel` **within your** `Array` **and add it's models to the** `group` **array**: ```ts export const MY_DYNAMIC_FORM_MODEL: DynamicFormControlModel[] = [ new DynamicFormGroupModel({ id: "basicFormGroup1", legend: "Form Group 1", group: [ new DynamicInputModel({ id: "basicGroupInput1-1", label: "Example Group Input 1-1", value: "Test 1-1" }), new DynamicInputModel({ id: "basicGroupInput1-2", label: "Example Group Input 1-2", value: "Test 1-2" }) ] }), new DynamicFormGroupModel({ id: "basicFormGroup2", legend: "Form Group 2", group: [ new DynamicInputModel({ id: "basicGroupInput2-1", label: "Example Group Input 2-1", value: "Test 2-1" }), new DynamicInputModel({ id: "basicGroupInput2-2", label: "Example Group Input 2-2", value: "Test 2-2" }) ] }) ]; ``` **2. Create a** `FormGroup` **and apply a** `DynamicFormControlComponent`: ```ts ngOnInit() { this.formGroup = this.formService.createFormGroup(this.formModel); } ``` ```ts
``` ## Form Arrays Sometimes forms need to allow the user to dynamically add multiple items of the same kind to it, e.g. addresses, products and so on. Particularly for this reason Angular 2 provides so called [**Form Arrays**](https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2). Fortunately, ng2 Dynamic Forms is capable of managing such nested form structures! **1. Add a** `DynamicFormArrayModel` **to your form model**: ```ts export const MY_DYNAMIC_FORM_MODEL: DynamicFormControlModel[] = [ new DynamicFormArrayModel({ id: "myFormArrayModel" }) ]; ``` **2. Add the** `createGroup` **property** to the `DynamicFormArrayModel` **and assign a function** to it which **returns the structure** of a single form array item: ```ts new DynamicFormArrayModel({ id: "myFormArray", initialCount: 5, createGroup: () => { return [ new DynamicInputModel({ id: "formArrayInput", label: "Form Array Input" }) ]; } }) ``` **3. As usual, create a** `FormGroup` **via** `DynamicFormService` **and bind it to your component template**: ```ts this.formGroup = this.formService.createFormGroup(this.formModel); ``` ```ts
``` **4. You can now easily modify your form array with** `DynamicFormService`: ```ts ngOnInit() { this.arrayControl = this.formGroup.get("myFormArray"); this.arrayModel = this.formService.findById("myFormArray", this.formModel); } addItem() { this.formService.addFormArrayGroup(this.arrayControl, this.arrayModel); } clear() { this.formService.clearFormArray(this.arrayControl, this.arrayModel); } ``` Alright, works like a charm! *But wait a minute... what if we want to append, let's say, a remove* `