Guest User

Untitled

a guest
Jan 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. this.mainForm = this.formBuilder.group({
  2. productType1: new FormArray([], CustomValidators.minSelectedCheckboxes()),
  3. productType2: new FormArray([],CustomValidators.minSelectedCheckboxes()),
  4. });
  5. }
  6.  
  7. static minSelectedCheckboxes(min: number = 1): ValidatorFn {
  8. const result = (formArray: FormArray): { [key: string]: boolean } | null => {
  9. const totalSelected = formArray.controls
  10. // get a list of checkbox values (boolean)
  11. .map((control) => control.value)
  12. // total up the number of checked checkboxes
  13. .reduce((prev, next) => next ? prev + next : prev, 0);
  14. // if the total is not greater than the minimum, return the error message
  15. return totalSelected >= min ? null : { required: true };
  16. };
  17. return result;
  18. }
Add Comment
Please, Sign In to add comment