{ "id": "guide/dynamic-form", "title": "Building dynamic forms", "contents": "\n\n\n
Many forms, such as questionaires, can be very similar to one another in format and intent.\nTo make it faster and easier to generate different versions of such a form,\nyou can create a dynamic form template based on metadata that describes the business object model.\nYou can then use the template to generate new forms automatically, according to changes in the data model.
\nThe technique is particularly useful when you have a type of form whose content must\nchange frequently to meet rapidly changing business and regulatory requirements.\nA typical use case is a questionaire. You might need to get input from users in different contexts.\nThe format and style of the forms a user sees should remain constant, while the actual questions you need to ask vary with the context.
\nIn this tutorial you will build a dynamic form that presents a basic questionaire.\nYou will build an online application for heroes seeking employment.\nThe agency is constantly tinkering with the application process, but by using the dynamic form\nyou can create the new forms on the fly without changing the application code.
\nThe tutorial walks you through the following steps.
\nThe form you create uses input validation and styling to improve the user experience.\nIt has a Submit button that is only enabled when all user input is valid, and flags invalid input with color coding and error messages.
\nThe basic version can evolve to support a richer variety of questions, more graceful rendering, and superior user experience.
\nSee the
Before doing this tutorial, you should have a basic understanding to the following.
\nTypeScript and HTML5 programming.
\nFundamental concepts of Angular app design.
\nBasic knowledge of reactive forms.
\nDynamic forms are based on reactive forms. To give the application access reactive forms directives, the root module imports ReactiveFormsModule
from the @angular/forms
library.
The following code from the example shows the setup in the root module.
\nA dynamic form requires an object model that can describe all scenarios needed by the form functionality.\nThe example hero-application form is a set of questions—that is, each control in the form must ask a question and accept an answer.
\nThe data model for this type of form must represent a question.\nThe example includes the DynamicFormQuestionComponent
, which defines a question as the fundamental object in the model.
The following QuestionBase
is a base class for a set of controls that can represent the question and its answer in the form.
From this base, the example derives two new classes, TextboxQuestion
and DropdownQuestion
,\nthat represent different control types.\nWhen you create the form template in the next step, you will instantiate these specific question types in order to render the appropriate controls dynamically.
The TextboxQuestion
control type presents a question and allows users to enter input.
The TextboxQuestion
control type will be represented in a form template using an <input>
element.\nThe type
attribute of the element will be defined based on the type
field specified in the options
argument (for example text
, email
, url
).
The DropdownQuestion
control presents a list of choices in a select box.
A dynamic form uses a service to create grouped sets of input controls, based on the form model.\nThe following QuestionControlService
collects a set of FormGroup
instances that consume the metadata from the question model. You can specify default values and validation rules.
The dynamic form itself will be represented by a container component, which you will add in a later step.\nEach question is represented in the form component's template by an <app-question>
tag, which matches an instance of DynamicFormQuestionComponent
.
The DynamicFormQuestionComponent
is responsible for rendering the details of an individual question based on values in the data-bound question object.\nThe form relies on a [formGroup]
directive to connect the template HTML to the underlying control objects.\nThe DynamicFormQuestionComponent
creates form groups and populates them with controls defined in the question model, specifying display and validation rules.
The goal of the DynamicFormQuestionComponent
is to present question types defined in your model.\nYou only have two types of questions at this point but you can imagine many more.\nThe ngSwitch
statement in the template determines which type of question to display.\nThe switch uses directives with the formControlName
and formGroup
selectors. Both directives are defined in ReactiveFormsModule
.
Another service is needed to supply a specific set of questions from which to build an individual form.\nFor this exercise you will create the QuestionService
to supply this array of questions from the hard-coded sample data.\nIn a real-world app, the service might fetch data from a backend system.\nThe key point, however, is that you control the hero job-application questions entirely through the objects returned from QuestionService
.\nTo maintain the questionnaire as requirements change, you only need to add, update, and remove objects from the questions
array.
The QuestionService
supplies a set of questions in the form of an array bound to @Input()
questions.
The DynamicFormComponent
component is the entry point and the main container for the form, which is represented using the <app-dynamic-form>
in a template.
The DynamicFormComponent
component presents a list of questions by binding each one to an <app-question>
element that matches the DynamicFormQuestionComponent
.
To display an instance of the dynamic form, the AppComponent
shell template passes the questions
array returned by the QuestionService
to the form container component, <app-dynamic-form>
.
The example provides a model for a job application for heroes, but there are\nno references to any specific hero question other than the objects returned by QuestionService
.\nThis separation of model and data allows you to repurpose the components for any type of survey\nas long as it's compatible with the question object model.
The form template uses dynamic data binding of metadata to render the form\nwithout making any hardcoded assumptions about specific questions.\nIt adds both control metadata and validation criteria dynamically.
\nTo ensure valid input, the Save button is disabled until the form is in a valid state.\nWhen the form is valid, you can click Save and the app renders the current form values as JSON.
\nThe following figure shows the final form.
\nDifferent types of forms and control collection
\n This tutorial shows how to build a a questionaire, which is just one kind of dynamic form.\nThe example uses FormGroup
to collect a set of controls.\nFor an example of a different type of dynamic form, see the section Creating dynamic forms in the Reactive Forms guide.\nThat example also shows how to use FormArray
instead of FormGroup
to collect a set of controls.
Validating user input
\nThe section Validating form input introduces the basics of how input validation works in reactive forms.
\nThe Form validation guide covers the topic in more depth.
\n