I have two select/dropdown fields in my form and second dropdown field renders based on a condition *ngIf="selectedStdntList?.packages"
. But in below code, it stops submit form even if the condition *ngIf="selectedStdntList?.packages"
is not true and second dropdown is not rendered on the screen.
student-list.component.html
<form [formGroup]="moveStudentForm" #formDirective="ngForm" (ngSubmit)="moveStudent();">
<uitk-form-field>
<label uitkLabel>Student List:</label>
<uitk-select id="my-required-reactive-select" formControlName="stdntListCtrl"
[itemList]="stdntListDropdown" [showError]="moveStudentForm?.controls?.stdntListCtrl?.errors?.required"
defaultLabel="Please Select" defaultLabelFlag="true">
</uitk-select>
<uitk-form-field-error
*ngIf="moveStudentForm?.controls?.stdntListCtrl?.errors?.required && moveStudentForm?.controls?.stdntListCtrl?.touched">
<span>Please select student list</span>
</uitk-form-field-error>
</uitk-form-field>
<uitk-form-field *ngIf="selectedStdntList?.packages">
<label uitkLabel>Package:</label>
<uitk-select id="my-required-reactive-select" formControlName="moveStudentPackageCtrl" [itemList]="packagesDropdown"
[showError]="moveStudentForm?.controls?.packageCtrl?.errors?.required" defaultLabel="Please Select"
defaultLabelFlag="true">
</uitk-select>
<uitk-form-field-error
*ngIf="moveStudentForm?.controls?.packageCtrl?.errors?.required && moveStudentForm?.controls?.packageCtrl?.touched">
<span>Please select package</span>
</uitk-form-field-error>
</uitk-form-field>
</form>
student-list.component.ts
@Component({
selector: 'stdnt-list',
templateUrl: './stdnt-list.component.html',
styleUrls: ['./stdnt-list.component.scss']
})
export class StdntListComponent implements OnInit {
stdntListDropdown: IUITKSelectItemProps[] = [];
moveStudentForm: FormGroup;
constructor(private dataService: DataService)
this.moveStudentForm = new FormGroup({
stdntListCtrl: new FormControl(null, Validators.required),
moveStudentPackageCtrl: new FormControl(null, Validators.required),
});
}
ngOnInit() {
this.stdntList = this.dataService.loadStudentData();
}
moveStudent() {
...
}
}
Desired output:
If second field condition *ngIf="selectedStdntList?.packages"
is true then only validator of this field should work, else only first field validator should work and form should be allowed to be submitted.