I have been struggling to validate a Reactive form with default "select" option to my select input box when there's no value selected. If a value is present in the form then I need to select the respective value.
I am using reactive form and below is my code implementation:
<select class="form-control" formControlName="environment" >
<option disabled [value]="null">-- Select --</option>
<option *ngFor="let ami of envs" [value]="ami.amiid">
{{ami.name}}
</option>
</select>
this.onlineTestForm = this._fb.group({
.
.
environment: ['', Validators.required],
.
.
});
//value is present in the model
(<FormControl>this.onlineTestForm.controls['environment'])
.setValue(this.model.environment, { onlySelf: true });
//value is not present in model
(<FormControl>this.onlineTestForm.controls['environment'])
.setValue('null', { onlySelf: true });
Is there a better way to achieve the above?