Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 : Validate child component form fields from the parent component

Problem statement :

Parent component having <form> tag and some <input> tags inside it, and child component also have some <input> tags, parent component has one <submit> and we are validating form fields on submit the form.

How to validate the child component <input> fields from parent component on submit the form ?

Requirement :

If a parent component has a form containing child components with input components in their template, then these input components should be validate on click if submit from the parent component.

Findings :

There are lot of posts in SO having same problem statement but did not find any suitable solution.All the below posts validate the whole form but my requirement is to validate each field in child component.

like image 849
Creative Learner Avatar asked Dec 26 '17 17:12

Creative Learner


People also ask

How do you find the value of a child component in the parent component?

We can get child component values in the parent component by creating a reference to the child component using the @ref directive in the Parent component. Using the reference instance, you can access the child component values in the parent.

How does a child component communicate with a parent component in Angular?

The child component uses the @Output() property to raise an event to notify the parent of the change. To raise an event, an @Output() must have the type of EventEmitter , which is a class in @angular/core that you use to emit custom events.


Video Answer


1 Answers

We can achieve it using template driven technique as well. Find below the steps :

  • From parent component to child component we have to pass submit button event.

    <button type="button" (click)="enterForm(parentForm)">Submit</button>
    

    Here, parentForm is the form reference.

  • call child component method using @ViewChild decorator from parent to pass submit button event on click of submit.

    @ViewChild('validateChildComponentForm') private ChildComponent: ChildComponent;
    
  • Pass the reference of child form using @ViewChild decorator into the child component.

    @ViewChild('smartyStreetForm') form;
    
    enterForm(parentForm) {
        this.submitted = true;
        this.ChildComponent.validateChildForm(this.submitted);
        if(!parentForm.valid || !this.childFormValidCheck) {
            return;
        } else {
           // success code comes here.
        }                
    }
    
  • Now in child component method we will check that if parent form is submitted and child component form is valid then emit true otherwise false into the parent component. we will use @Output decorator to emit the isChildFormValid value into the parent component.

    @Output() isChildFormValid: EventEmitter<any> = new EventEmitter<any>();
    
    public validateChildForm(data: any) {
        if (data === true) {
            if(this.form.valid === true) {
                this.isChildFormValid.emit(true);
            } else {
                this.isChildFormValid.emit(false);
            }
        }
    } 
    
  • Now in parent component we will get the isChildFormValid value.

    private isChildFormValid(formValid: any) {
        this.childFormValidCheck = formValid;
    }
    

Pictorial representation :

enter image description here

like image 170
Creative Learner Avatar answered Oct 23 '22 13:10

Creative Learner