0

I would like to get all errors from a form. I tried to use the live example that came with Angular documentation, and I modified it adding required to the first field:

createForm() {
    this.heroForm = this.fb.group({
        name: ['', Validators.required],
        secretLairs: this.fb.array([]),
        power: '',
        sidekick: ''
    });
}

https://plnkr.co/edit/6b1paWOlKtXnDn1VVCyP

As you can see, if you empty the name field it triggers the required validator and control related to name field has an errors object... but errors property of myForm object is still null. Why?

Shouldn't it contain an object with all errors that are triggered by validators of the children controls? So what does errors property stand for?

smartmouse
  • 13,912
  • 34
  • 100
  • 166

1 Answers1

1

You would have to loop through the controls of the form and see if they contain errors one by one. And I think you can actually have validators on the group it self, the same way you would have validation on a single form control.

realappie
  • 4,656
  • 2
  • 29
  • 38
  • It should technically contain the errors of its siblings, but in my case it's not doing that either. I don't know if this is intended behavior or a bug, but if you simply want to achieve your goal of collecting all errors in the form you could check [this](https://stackoverflow.com/questions/40680321/get-all-validation-errors-from-angular-2-formgroup) question out – realappie Jun 16 '17 at 11:07
  • 1
    I can confirm this is intended behavior, have a look at [this](https://github.com/angular/angular/issues/11530) issue – realappie Jun 16 '17 at 11:09