I have a FormControl
without an initial value but I would like to add Validators.required
to it. The initial value is the first argument, Do I have to give it an initial value? (empty string, false, etc)
this.contactForm = new FormGroup({
name: new FormControl('', Validators.required),
email: new FormControl('', [Validators.required, CustomEmailValidator]),
optInEmails: new FormControl(false, Validators.required)
})
The constructor for the Form Control in Angular is as follows:
constructor(formState: any = null, validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[])
The formState attribute sets the initial value to the FormControl Object you create.
The values for the attributes need to be specified in the same order as the constructor. Hence, you can define your formControl as optInEmails: new FormControl(null, Validators.required)