Rapid form development library for Angular
NG Dynamic Forms is a rapid form development library based on the official Angular dynamic forms guide.
It fully automates form UI creation by introducing a set of maintainable form control models and dynamic form control components
Out of the box support is provided for all popular UI libraries including Material, ngx-bootstrap, NG Bootstrap, Foundation, Ionic and PrimeNG.
Explore the live sample and the API documentation!
1. Install the core package:
npm i @ng-dynamic-forms/core -S2. Install a UI package and its peer dependencies:
npm i @ng-dynamic-forms/ui-material -S1. Clone the Git repository:
git clone https://github.com/udos86/ng-dynamic-forms.git
cd ng-dynamic-forms2. Install the dependencies:
npm i3. Build the library:
npm run build:lib4. Run the application:
ng serve1. Import the UI module:
import { DynamicFormsMaterialUIModule } from "@ng-dynamic-forms/ui-material";
@NgModule({
imports: [
ReactiveFormsModule,
DynamicFormsMaterialUIModule
]
})
export class AppModule {}2. Define your form model:
…3. Create a FormGroup via DynamicFormService:
import { MY_FORM_MODEL } from "./my-dynamic-form.model";
import { DynamicFormModel, DynamicFormService } from "@ng-dynamic-forms/core";
export class MyDynamicFormComponent {
formModel: DynamicFormModel = MY_FORM_MODEL;
formGroup = this.formService.createFormGroup(this.formModel);
constructor(private formService: DynamicFormService) {}
}4. Add a DynamicFormComponent to your template and bind its [group] and [model] property:
NG Dynamic Forms is built to provide solid yet unobtrusive support for a variety of common UI libraries:
You can instantly plug in your favorite form controls by installing the appropriate package and its peer dependencies:
npm i @ng-dynamic-forms/ui- -SNow just import the UI module:
@NgModule({
imports: [
ReactiveFormsModule,
DynamicFormsMaterialUIModule
]
})
export class AppModule {}For creating the form markup all UI modules come with a DynamicFormComponent that can easily be added to
your component template:
Alternatively you can directly make use of a specific DynamicFormControlComponent to gain more control over rendering:
Due to technical restrictions or external dependencies still being in development the support of major form controls varies among UI packages. See the following compatibility table:
| | ui-basic | ui-ngx-bootstrap | ui-foundation | ui-ionic | ui-material | ui-ng-bootstrap | ui-primeng | |---------------- |:--------: |:----------------: |:-------------: |:--------: |:-----------: |:---------------: |:----------: | | Checkbox | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Checkbox Group | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Colorpicker | **** | ✗ | **** | **** | **** | **** | ✓ | | Datepicker | * | ✓ | * | ✓ | * | ✓ | ✓ | | Editor | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ | | File Upload | ** | ** | ** | ✗ | ** | ** | ** | | Input | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Radio Group | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Rating | ✗ | ✓ | ✗ | ✗ | ✗ | ✗ | ✓ | | Select | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Slider | *** | *** | *** | ✓ | ✓ | *** | ✓ | | Switch | ✗ | ✗ | ✓ | ✓ | ✓ | ✗ | ✓ | | Textarea | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Timepicker | * | ✓ | * | ✓ | * | ✓ | ✓ |
*) datetime 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"
****) color picker controls can be achieved using a DynamicInputModel with inputType: "color"
In order to improve clarity it's often considered good practice to group forms into several logical fieldset sections.
Thus NG Dynamic Forms supports nesting of form groups out of the box!
1. Declare a DynamicFormGroupModel within your form model and add it's models to the group array:
…2. Create a FormGroup and add a DynamicFormComponent:
formGroup = this.formService.createFormGroup(this.formModel);
3. To manipulate an existing DynamicFormGroupModel you can simply use DynamicFormService:
addFormGroupControl(...)insertFormGroupControl(...)moveFormGroupControl(...)removeFormGroupControl(...)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 provides so called Form Arrays.
Fortunately, NG Dynamic Forms is capable of managing such nested form structures!
1. Add a DynamicFormArrayModel to your form model:
export const MY_FORM_MODEL: DynamicFormModel = [
new DynamicFormArrayModel({
id: "myFormArray"
})
];2. Add the groupFactory property to the DynamicFormArrayModel and assign a function to it which returns
the structure of a single form array item:
new DynamicFormArrayModel({
id: "myFormArray",
initialCount: 5,
groupFactory: () => {
return [
new DynamicInputModel({
id: "myInput",
label: "My Input"
})
];
}
})3. Create a FormGroup via DynamicFormService and bind it to your component template:
this.formGroup = this.formService.createFormGroup(this.formModel);
Add item
Remove all items
4. You can now easily modify your form array with DynamicFormService:
ngOnInit() {
this.formArrayModel = this.formService.findModelById("myFormArray", this.formModel);
this.formArrayControl = this.formService.findControlByModel(this.formArrayModel, this.formGroup);
}
addItem() {
this.formService.addFormArrayGroup(this.formArrayControl, this.formArrayModel);
this.formService.detectChanges();
}
clear() {
this.formService.clearFormArray(this.formArrayControl, this.formArrayModel);
this.formService.detectChanges();
}Never forget to trigger change detection via detectChanges when updating a form at runtime!
Alright, works like a charm!
But what if we want to append an additional remove `` for each array group?
Particularly for this case you can add a `` and declare some custom content that is rendered equally for all form array groups:
Label
Whenever a `` is applied to a DynamicFormArrayModel, NgTemplateOutletContext is internally bound to
the associated DynamicFormArrayGroupModel.
That means you can access the group object and it's properties by either declaring a local default template variable or individual local template variables.
see chapter on Custom Templates
Remove Item
Add Item
This is extremely useful when you'd like to implement a remove or insert function:
removeItem(context: DynamicFormArrayModel, index: number) {
this.formService.removeFormArrayGroup(index, this.formArrayControl, context);
this.formService.detectChanges();
}
insertItem(context: DynamicFormArrayModel, index: number) {
this.formService.insertFormArrayGroup(index, this.formArrayControl, context);
this.formService.detectChanges();
}Using DynamicFormService again, you can even change the order of the groups in a form array dynamically:
this.formService.moveFormArrayGroup(index, -1, this.formArrayControl, context);
this.formService.detectChanges();When using a NG Dynamic Forms UI package, e.g. ui-bootstrap, all essential form classes of the underlying CSS library
(like form-group or form-control) are automatically put in place for you in t
No open issues yet, or sync has not completed.