11import { Component , OnInit } from '@angular/core' ;
2- import { FormControl , FormGroup , Validators } from '@angular/forms' ;
2+ import { AbstractControl , FormArray , FormControl , FormGroup , Validators } from '@angular/forms' ;
33import { TranslateService } from '@ngx-translate/core' ;
44
5+ function minCheckSelected ( size : number ) {
6+ return ( control : AbstractControl ) => {
7+ const values = control . value as ( boolean | undefined ) [ ] ;
8+ const selected = values . filter ( v => ! ! v ) . length ;
9+ if ( selected < size ) {
10+ return { checkBoxes : true } ;
11+ }
12+ return null ;
13+ } ;
14+ }
15+
516@Component ( {
617 selector : 'app-lazy-form' ,
718 templateUrl : './lazy-form.component.html' ,
@@ -11,10 +22,18 @@ export class LazyFormComponent implements OnInit {
1122
1223 heroForm : FormGroup ;
1324
25+ boxesInfo = [
26+ 'a' ,
27+ 'b' ,
28+ 'c' ,
29+ 'd'
30+ ] ;
31+
1432 constructor ( private translateService : TranslateService ) {
1533 this . heroForm = new FormGroup ( {
1634 name : new FormControl ( null , [ Validators . required , Validators . minLength ( 4 ) ] ) ,
17- surname : new FormControl ( null , [ Validators . required , Validators . maxLength ( 1000 ) ] )
35+ surname : new FormControl ( null , [ Validators . required , Validators . maxLength ( 1000 ) ] ) ,
36+ checkBoxes : new FormArray ( this . boxesInfo . map ( a => new FormControl ( ) ) , [ minCheckSelected ( 1 ) ] )
1837 } ) ;
1938 }
2039
0 commit comments