In ts file, declare a property for the form, which type should be FormGroup: such as: signupForm: FormGroup;
Initiate the form in ngOnInit() method:
signupForm = new FormGroup({
propertyName1: new FormControl(defaultName, validator, validator),
propertyName2: new FormControl(defaultName, Validators.required / [Validators.required, Validators.email, …], validator),
…});
synchronize the form you create with the form in template
binding the created form to template form in <form> tag with formGroup directive, such as: <form [formGroup] = “signupFom”> … </form>
sign each form control you create to template form control with formControlName directive, such as: <input … formControlName = “propertyName1” …>
To access each form control object, using get(propertyName), such as:
signupForm.get(‘username’).valid
signupForm.get(‘username’).touched
signupForm.valid // get the whole form validation
FormArray is an array holding a set of form controls, can be used to add new controls form to the form by user.
create your own validators, validator just a function, you need to create a function, return {errorName: boolean}, then put it to form registration part, the second argument: