Search code examples
formsangularangular2-forms

How to validate the child component NgForms having 3 different NgForm from the parent component in Angular4


I have a scenario where I am accessing two different NgForms one within Parent form #parentform and other in the Child component #childForm & #childForm1, and i want to check the validity of the controls of child form wether valid or not in parent component form. How to do this in angular4.

I also followed this link: Check if a form is valid from a parent component using Angular 4

everytime i am getting undefined for the reference of child component form.

My code is something like this.

parent.component.html

    <form class="form-wrapper" (ngSubmit)="parentForm.form.valid && save()" #parentForm="ngForm" novalidate>
        <input id="firstName" type="text" placeholder="" class="validate" name="firstName" [(ngModel)]="firstname_" #firstName="ngModel"                         required>
    </form>
    <child-component></child-component>

child.component.html

  <form class="form-wrapper" (ngSubmit)="childForm.form.valid && save()" #childForm="ngForm" novalidate>
            <input id="phoneNumber" type="text" placeholder="" class="validate" name="phoneNumber" [(ngModel)]="phone_" #phoneNumber="ngModel"                       required>
 </form>

 <form class="form-wrapper" (ngSubmit)="childForm1.form.valid && save()" #childForm1="ngForm" novalidate>
            <input id="mobileNumber" type="text" placeholder="" class="validate" name="mobileNumber" [(ngModel)]="mobile_" #mobileNumber="ngModel" required>
 </form>

Now i want to validate the child component form "childForm" & "childForm1" valid or not in parent form.

Same is reproduced at https://stackblitz.com/edit/angular-cjorjz...


Solution

  • So what you want, is to pass the parentForm.form.status to the child with an @Input().

    In parent html:

    <child-component [parent]="parentForm.form.status"></child-component>
    

    Then in your child:

    @Input() parent: any;
    private boolean: boolean = false;
    
    ngOnChanges(changes: any) {
      if(changes.dataSet.currentValue === 'VALID'){
        this.boolean = true;
      }
      else { this.boolean = false; }
    }
    

    And to check it console.log(this.boolean) it or put {{boolean}} in html. Or childForm.form.valid && save() && boolean in html.

    EDIT

    To send the validation back:

    In the child component you have to tigger the @Output() so use a change event on the html:

    @Output valid: EventEmitter<any> = new EventEmitter<any>();
    
    private checkValid(_childForm: string){
      if(_childForm === 'VALID'){
        this.valid.emit(true);
      }
      else { this.valid.emit(false);
    }
    

    In html to all your child formsfield:

    (ngModelChange)="checkValid(childForm.form.status)"
    

    In your parent html:

    <child-component [parent]="parentForm.form.status" (valid)="setValidChild($event)"></child-component>
    

    In the parent component:

    private childBoolean: boolean= false;
    
    private setValidChild(_boolean: boolean){
      this.childBoolean = _boolean;
    }