Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this.mainForm = this.formBuilder.group({
- productType1: new FormArray([], CustomValidators.minSelectedCheckboxes()),
- productType2: new FormArray([],CustomValidators.minSelectedCheckboxes()),
- });
- }
- static minSelectedCheckboxes(min: number = 1): ValidatorFn {
- const result = (formArray: FormArray): { [key: string]: boolean } | null => {
- const totalSelected = formArray.controls
- // get a list of checkbox values (boolean)
- .map((control) => control.value)
- // total up the number of checked checkboxes
- .reduce((prev, next) => next ? prev + next : prev, 0);
- // if the total is not greater than the minimum, return the error message
- return totalSelected >= min ? null : { required: true };
- };
- return result;
- }
Add Comment
Please, Sign In to add comment