14

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.

8
  • Use model driven form. Because it will check model values not form names of input. Commented Dec 26, 2017 at 17:14
  • I had a very similar requirement, but quite more complex because I had to validate a hierarchy forms, children, nested components and so on. It is very easy to validate using Reactive forms approach angular.io/guide/reactive-forms
    – Daniel C.
    Commented Dec 26, 2017 at 17:27
  • Are you facing problem with plugins, like auto-complete or datepiker? Commented Dec 26, 2017 at 17:39
  • stackoverflow.com/a/47695731/4834833 read this. Here a piece of code which added after a given parent element. I hope something help you. Commented Dec 26, 2017 at 17:44
  • Can you post a sample application so we can have more context? Commented Dec 28, 2017 at 10:57

1 Answer 1

16

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

5
  • 2
    A lot of code to achieve this. feels like this should be more simple. Commented Feb 12, 2018 at 15:31
  • 4
    Can you please provide your more simple solution as an answer in this post. Commented Feb 12, 2018 at 16:19
  • Well I actually didn't have one... I was just saying it should be more simple. Although I am currently reading through this article that looks rather promising. Commented Feb 12, 2018 at 16:24
  • blog.thoughtram.io/angular/2016/07/27/… Commented Feb 12, 2018 at 16:25
  • but where are error messages at the bottom of the fields?
    – GlacialMan
    Commented Apr 24, 2019 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.