Skip to content

Commit 2118677

Browse files
ricardochlSplaktar
authored andcommitted
translate: translations for forms guide
Fixes #30
1 parent cbba3ab commit 2118677

13 files changed

+2697
-874
lines changed

adev-es/src/app/sub-navigation-data.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,35 +396,35 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
396396
],
397397
},
398398
{
399-
label: 'Forms',
399+
label: 'Formularios',
400400
children: [
401401
{
402-
label: 'Overview',
402+
label: 'Visión general',
403403
path: 'guide/forms',
404404
contentPath: 'guide/forms/overview',
405405
},
406406
{
407-
label: 'Reactive forms',
407+
label: 'Formularios reactivos',
408408
path: 'guide/forms/reactive-forms',
409409
contentPath: 'guide/forms/reactive-forms',
410410
},
411411
{
412-
label: 'Strictly typed reactive forms',
412+
label: 'Formularios reactivos estrictamente tipados',
413413
path: 'guide/forms/typed-forms',
414414
contentPath: 'guide/forms/typed-forms',
415415
},
416416
{
417-
label: 'Template-driven forms',
417+
label: 'Formularios basados en plantillas',
418418
path: 'guide/forms/template-driven-forms',
419419
contentPath: 'guide/forms/template-driven-forms',
420420
},
421421
{
422-
label: 'Validate form input',
422+
label: 'Validar entrada de formularios',
423423
path: 'guide/forms/form-validation',
424424
contentPath: 'guide/forms/form-validation',
425425
},
426426
{
427-
label: 'Building dynamic forms',
427+
label: 'Construir formularios dinámicos',
428428
path: 'guide/forms/dynamic-forms',
429429
contentPath: 'guide/forms/dynamic-forms',
430430
},
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Building dynamic forms
2+
3+
Many forms, such as questionnaires, can be very similar to one another in format and intent.
4+
To make it faster and easier to generate different versions of such a form, you can create a _dynamic form template_ based on metadata that describes the business object model.
5+
Then, use the template to generate new forms automatically, according to changes in the data model.
6+
7+
The technique is particularly useful when you have a type of form whose content must change frequently to meet rapidly changing business and regulatory requirements.
8+
A typical use-case is a questionnaire.
9+
You might need to get input from users in different contexts.
10+
The format and style of the forms a user sees should remain constant, while the actual questions you need to ask vary with the context.
11+
12+
In this tutorial you will build a dynamic form that presents a basic questionnaire.
13+
You build an online application for heroes seeking employment.
14+
The agency is constantly tinkering with the application process, but by using the dynamic form
15+
you can create the new forms on the fly without changing the application code.
16+
17+
The tutorial walks you through the following steps.
18+
19+
1. Enable reactive forms for a project.
20+
1. Establish a data model to represent form controls.
21+
1. Populate the model with sample data.
22+
1. Develop a component to create form controls dynamically.
23+
24+
The form you create uses input validation and styling to improve the user experience.
25+
It has a Submit button that is only enabled when all user input is valid, and flags invalid input with color coding and error messages.
26+
27+
The basic version can evolve to support a richer variety of questions, more graceful rendering, and superior user experience.
28+
29+
## Enable reactive forms for your project
30+
31+
Dynamic forms are based on reactive forms.
32+
33+
To give the application access reactive forms directives, import `ReactiveFormsModule` from the `@angular/forms` library into the necessary components.
34+
35+
<docs-code-multifile>
36+
<docs-code header="dynamic-form.component.ts" path="adev/src/content/examples/dynamic-form/src/app/dynamic-form.component.ts"/>
37+
<docs-code header="dynamic-form-question.component.ts" path="adev/src/content/examples/dynamic-form/src/app/dynamic-form-question.component.ts"/>
38+
</docs-code-multifile>
39+
40+
## Create a form object model
41+
42+
A dynamic form requires an object model that can describe all scenarios needed by the form functionality.
43+
The example hero-application form is a set of questions — that is, each control in the form must ask a question and accept an answer.
44+
45+
The data model for this type of form must represent a question.
46+
The example includes the `DynamicFormQuestionComponent`, which defines a question as the fundamental object in the model.
47+
48+
The following `QuestionBase` is a base class for a set of controls that can represent the question and its answer in the form.
49+
50+
<docs-code header="src/app/question-base.ts" path="adev/src/content/examples/dynamic-form/src/app/question-base.ts"/>
51+
52+
### Define control classes
53+
54+
From this base, the example derives two new classes, `TextboxQuestion` and `DropdownQuestion`, that represent different control types.
55+
When you create the form template in the next step, you instantiate these specific question types in order to render the appropriate controls dynamically.
56+
57+
The `TextboxQuestion` control type is represented in a form template using an `<input>` element. It presents a question and lets users enter input. The `type` attribute of the element is defined based on the `type` field specified in the `options` argument (for example `text`, `email`, `url`).
58+
59+
<docs-code header="question-textbox.ts" path="adev/src/content/examples/dynamic-form/src/app/question-textbox.ts"/>
60+
61+
The `DropdownQuestion` control type presents a list of choices in a select box.
62+
63+
<docs-code header="question-dropdown.ts" path="adev/src/content/examples/dynamic-form/src/app/question-dropdown.ts"/>
64+
65+
### Compose form groups
66+
67+
A dynamic form uses a service to create grouped sets of input controls, based on the form model.
68+
The following `QuestionControlService` collects a set of `FormGroup` instances that consume the metadata from the question model.
69+
You can specify default values and validation rules.
70+
71+
<docs-code header="src/app/question-control.service.ts" path="adev/src/content/examples/dynamic-form/src/app/question-control.service.ts"/>
72+
73+
## Compose dynamic form contents
74+
75+
The dynamic form itself is represented by a container component, which you add in a later step.
76+
Each question is represented in the form component's template by an `<app-question>` tag, which matches an instance of `DynamicFormQuestionComponent`.
77+
78+
The `DynamicFormQuestionComponent` is responsible for rendering the details of an individual question based on values in the data-bound question object.
79+
The form relies on a [`[formGroup]` directive](api/forms/FormGroupDirective "API reference") to connect the template HTML to the underlying control objects.
80+
The `DynamicFormQuestionComponent` creates form groups and populates them with controls defined in the question model, specifying display and validation rules.
81+
82+
<docs-code-multifile>
83+
<docs-code header="dynamic-form-question.component.html" path="adev/src/content/examples/dynamic-form/src/app/dynamic-form-question.component.html"/>
84+
<docs-code header="dynamic-form-question.component.ts" path="adev/src/content/examples/dynamic-form/src/app/dynamic-form-question.component.ts"/>
85+
</docs-code-multifile>
86+
87+
The goal of the `DynamicFormQuestionComponent` is to present question types defined in your model.
88+
You only have two types of questions at this point but you can imagine many more.
89+
The `@switch` block in the template determines which type of question to display.
90+
The switch uses directives with the [`formControlName`](api/forms/FormControlName "FormControlName directive API reference") and [`formGroup`](api/forms/FormGroupDirective "FormGroupDirective API reference") selectors.
91+
Both directives are defined in `ReactiveFormsModule`.
92+
93+
### Supply data
94+
95+
Another service is needed to supply a specific set of questions from which to build an individual form.
96+
For this exercise you create the `QuestionService` to supply this array of questions from the hard-coded sample data.
97+
In a real-world app, the service might fetch data from a backend system.
98+
The key point, however, is that you control the hero job-application questions entirely through the objects returned from `QuestionService`.
99+
To maintain the questionnaire as requirements change, you only need to add, update, and remove objects from the `questions` array.
100+
101+
The `QuestionService` supplies a set of questions in the form of an array bound to `input()` questions.
102+
103+
<docs-code header="src/app/question.service.ts" path="adev/src/content/examples/dynamic-form/src/app/question.service.ts"/>
104+
105+
## Create a dynamic form template
106+
107+
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.
108+
109+
The `DynamicFormComponent` component presents a list of questions by binding each one to an `<app-question>` element that matches the `DynamicFormQuestionComponent`.
110+
111+
<docs-code-multifile>
112+
<docs-code header="dynamic-form.component.html" path="adev/src/content/examples/dynamic-form/src/app/dynamic-form.component.html"/>
113+
<docs-code header="dynamic-form.component.ts" path="adev/src/content/examples/dynamic-form/src/app/dynamic-form.component.ts"/>
114+
</docs-code-multifile>
115+
116+
### Display the form
117+
118+
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>`.
119+
120+
<docs-code header="app.component.ts" path="adev/src/content/examples/dynamic-form/src/app/app.component.ts"/>
121+
122+
This separation of model and data lets you repurpose the components for any type of survey, as long as it's compatible with the _question_ object model.
123+
124+
### Ensuring valid data
125+
126+
The form template uses dynamic data binding of metadata to render the form without making any hardcoded assumptions about specific questions.
127+
It adds both control metadata and validation criteria dynamically.
128+
129+
To ensure valid input, the _Save_ button is disabled until the form is in a valid state.
130+
When the form is valid, click _Save_ and the application renders the current form values as JSON.
131+
132+
The following figure shows the final form.
133+
134+
<img alt="Dynamic-Form" src="assets/images/guide/dynamic-form/dynamic-form.png">
135+
136+
## Next steps
137+
138+
<docs-pill-row>
139+
<docs-pill title="Validating form input" href="guide/forms/reactive-forms#validating-form-input" />
140+
<docs-pill title="Form validation guide" href="guide/forms/form-validation" />
141+
</docs-pill-row>

0 commit comments

Comments
 (0)