{"id":3174,"date":"2024-04-09T16:00:03","date_gmt":"2024-04-09T14:00:03","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3174"},"modified":"2024-04-09T16:00:03","modified_gmt":"2024-04-09T14:00:03","slug":"how-to-retrieve-the-list-of-form-controls-that-have-validation-errors-in-a-formgroup-angular-reactive-forms","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2024\/04\/09\/how-to-retrieve-the-list-of-form-controls-that-have-validation-errors-in-a-formgroup-angular-reactive-forms\/","title":{"rendered":"How to retrieve the list of form controls that have validation errors in a formGroup (Angular Reactive Forms)"},"content":{"rendered":"\n<p>Sometimes in the development process, we may want to get the list of form controls that have validation errors in Angular Reactive Forms. To solve this problem we can use the Error property of AbstractControl class.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to achieve this:<\/p>\n\n\n\n<p>Let\u00b4s say, we have the following template :  (<strong><em>app-component.html<\/em><\/strong>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form  &#91;formGroup]=\"myFormGroup\" (ngSubmit)=\"onSubmit()\">\n  &lt;!-- Here we have a list of controls with formControlName -->\n ...    \n &lt;form-field>\r\n              &lt;label>ProjektType&lt;\/sbb-label>\r\n               &lt;input formControlName=\"type\"\/>\r\n    &lt;\/form-field>\n   &lt;form-field>\r\n              &lt;label>Manager&lt;\/sbb-label>\r\n               &lt;input formControlName=\"manager\"\/>\r\n    &lt;\/form-field>\n  ...\n&lt;\/form><\/code><\/pre>\n\n\n\n<p>To retrieve the list of form controls that have validation errors, let us create this function in the component ((<strong><em>app-component.ts<\/em><\/strong>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Assuming myFormGroup is our FormGroup instance\r\nObject.keys(this.myFormGroup.controls).forEach(controlName => {\r\n  const control = this.myFormGroup.get(controlName);\r\n  if (control?.errors) {\r\n    console.log('Control name with error: ', controlName);\r\n    console.log('Error details: ', control.errors);\r\n  }\r\n});\r\n<\/code><\/pre>\n\n\n\n<p>In the above example, we iterate through each control in the formGroup using <em><strong>Object.keys(this.myFormGroup.controls)<\/strong><\/em>, and then for each control, we check if it has any errors using <em><strong>control.errors<\/strong><\/em>. If errors exist, we can log the control name and the error details.<\/p>\n\n\n\n<p>We can use this approach to retrieve the names of form controls that have validation errors and perform any necessary actions based on the error information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes in the development process, we may want to get the list of form controls that have validation errors in Angular Reactive Forms. To solve this problem we can use the Error property of AbstractControl class. Here&#8217;s an example of how to achieve this: Let\u00b4s say, we have the following template : (app-component.html) To retrieve [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,5,83],"tags":[820,815,817,819,816,818,814],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3174"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3174"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3174\/revisions"}],"predecessor-version":[{"id":3175,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3174\/revisions\/3175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}