@@ -2,6 +2,7 @@ import {Component, OnInit, ViewChild} from '@angular/core';
22import { AbstractControl , FormArray , FormControl , FormGroup , Validators } from '@angular/forms' ;
33import { TranslateService } from '@ngx-translate/core' ;
44import { ValidationContextComponent } from '@xtream/ngx-validation-errors' ;
5+ import { distinctUntilChanged , tap } from 'rxjs/operators' ;
56
67function minCheckSelected ( size : number ) {
78 return ( control : AbstractControl ) => {
@@ -21,7 +22,10 @@ function minCheckSelected(size: number) {
2122} )
2223export class LazyFormComponent implements OnInit {
2324
24- @ViewChild ( 'firstForm' , { read : ValidationContextComponent , static : true } ) validationContext : ValidationContextComponent ;
25+ @ViewChild ( 'firstForm' , {
26+ read : ValidationContextComponent ,
27+ static : true
28+ } ) validationContext : ValidationContextComponent ;
2529
2630 heroForm : FormGroup ;
2731
@@ -35,9 +39,21 @@ export class LazyFormComponent implements OnInit {
3539 constructor ( private translateService : TranslateService ) {
3640 this . heroForm = new FormGroup ( {
3741 name : new FormControl ( null , [ Validators . required , Validators . minLength ( 4 ) ] ) ,
38- surname : new FormControl ( null , [ Validators . required , Validators . maxLength ( 1000 ) ] ) ,
42+ surname : new FormControl ( { value : null , disabled : true } , [ Validators . required , Validators . maxLength ( 1000 ) ] ) ,
3943 checkBoxes : new FormArray ( this . boxesInfo . map ( a => new FormControl ( ) ) , [ minCheckSelected ( 1 ) ] )
4044 } ) ;
45+
46+ this . heroForm . valueChanges . pipe (
47+ distinctUntilChanged ( ) ,
48+ tap ( v => {
49+ console . debug ( 'v' , v ) ;
50+ if ( v . name ) {
51+ this . heroForm . controls [ 'surname' ] . enable ( { emitEvent : false } ) ;
52+ } else {
53+ this . heroForm . controls [ 'surname' ] . disable ( { emitEvent : false } ) ;
54+ }
55+ } )
56+ ) . subscribe ( )
4157 }
4258
4359 ngOnInit ( ) : void {
0 commit comments