In my Angular app I have a complex entity that can be created in multiple ways: manually, by uploading json files, by importing from another url, etc.
For each case described above, I have build a separate form, using Reactive Forms, in separate components.
All the other form fields are different, except the field for the atrribute called name
.
The formControl attached to the name
attribute has the following definition:
name: new FormControl(this.initialValue, [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
])
Now, I don't like that I have to repeat the same definition for this form control on every reactive form constructor, which, as I said before, are in separate components.
Here are some code snippets:
this.formA = new FormGroup({
name: new FormControl(this.initialValue, [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
]),
fooControl1: new FormControl(''),
fooControl2: new FormControl('')
});
this.formB = new FormGroup({
name: new FormControl(this.initialValue, [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
]),
barControl: new FormControl('')
});
I have build a simple Stackblitz example: https://stackblitz.com/edit/angular-tyjnlq. (for simplification, in the example the forms are in same component)
Question: How can I save this form control and include it in every form?