{"id":2126,"date":"2024-07-31T17:40:38","date_gmt":"2024-08-01T00:40:38","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2126"},"modified":"2024-07-31T17:40:39","modified_gmt":"2024-08-01T00:40:39","slug":"formgroup-all-you-need-to-know","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/formgroup-all-you-need-to-know\/","title":{"rendered":"FormGroup: All you need to know"},"content":{"rendered":"\n<p>Recently, during an <a href=\"https:\/\/courses.angulartraining.com\/course\/angular-accelerator-program\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular Accelerator coaching call<\/a>, my client asked what is the point of using <code><a href=\"https:\/\/angular.dev\/api\/forms\/FormGroup?tab=usage-notes\" target=\"_blank\" rel=\"noopener\" title=\"\">FormGroup<\/a><\/code>. I then realized I had never written about <code>FormGroup<\/code>, so here is a post to fix that.<\/p>\n\n\n\n<p>When using <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/reactive-or-template-driven-forms\/\" target=\"_blank\" rel=\"noopener\" title=\"\">reactive forms<\/a>, we always have at least one <code>FormGroup<\/code>, which happens to be the entire form. This doesn&#8217;t mean that Form Groups are only designed for that purpose: A complex form can be divided into multiple groups. For instance, if we have a form where a user is supposed to enter their name, address, birth date, etc., we could group some of these controls into <code>FormGroups<\/code>.<\/p>\n\n\n\n<p>What do we get out of that? A few different things:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Form groups aggregate the <strong>validation state of all individual controls into a group state<\/strong>. If an address is made of 4 controls (street, city, zip code, country), then the form group for these 4 controls will be <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/basic-form-validation-with-angular\/\" target=\"_blank\" rel=\"noopener\" title=\"\">invalid<\/a> when any controls are invalid, touched when any control is <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/basic-form-validation-with-angular\/\" target=\"_blank\" rel=\"noopener\" title=\"\">touched<\/a>, etc.<\/li>\n\n\n\n<li>Form groups <strong>aggregate actions on all individual controls within the group<\/strong>. Want to disable address entry? Instead of disabling all 4 controls one by one, you can do <code>addressGroup.disable()<\/code> , which is a lot less error-prone (you can&#8217;t forget one of these controls)<\/li>\n\n\n\n<li>Form groups store the value of all controls as a single object. In my example with 4 controls for an address, accessing <code>addressGroup.getValue()<\/code> would return:<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"315\" height=\"184\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-10.png\" alt=\"\" class=\"wp-image-2127\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-10.png 315w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-10-300x175.png 300w\" sizes=\"auto, (max-width: 315px) 100vw, 315px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>So, the main benefit is to have grouped actions and status updates for a bunch of form controls. Let&#8217;s take another example with actual code (you can find the <a href=\"https:\/\/stackblitz.com\/edit\/at-formgroup?file=src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">full code base on Stackblitz here<\/a>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"256\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-11.png\" alt=\"\" class=\"wp-image-2128\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-11.png 408w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-11-300x188.png 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If I use the following expression in my template to see the value of my <code>userInfo<\/code>, here&#8217;s what I get:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"442\" height=\"38\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-12.png\" alt=\"\" class=\"wp-image-2129\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-12.png 442w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-12-300x26.png 300w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"264\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/screenshot.gif\" alt=\"\" class=\"wp-image-2130\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As you can see, my form group creates an object that updates when individual control values change. Perfect! Now, let&#8217;s click a button that sets the form group value to an object:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"31\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-13.png\" alt=\"\" class=\"wp-image-2131\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-13.png 852w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-13-300x11.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-13-768x28.png 768w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"43\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-14.png\" alt=\"\" class=\"wp-image-2132\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-14.png 739w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-14-300x17.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"264\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/screenshot-1.gif\" alt=\"\" class=\"wp-image-2133\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now, I can use multiple values with one line of code. There is no need to call <code>setValue()<\/code> on all controls one by one. That&#8217;s cool. What if I want to apply a &#8220;diff&#8221; to my group and not set the entire thing? Say I wanted to be younger but not change my name or anything else:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"92\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-15.png\" alt=\"\" class=\"wp-image-2134\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-15.png 595w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-15-300x46.png 300w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"264\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/screenshot-2.gif\" alt=\"\" class=\"wp-image-2135\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now, I can &#8220;patch&#8221; my form group with some localized changes, and I don&#8217;t need to worry about the other values of the controls of that group. That&#8217;s the magic of <code>formGroup.patchValue()<\/code>.<\/p>\n\n\n\n<p>You can also reset the entire group with <code>formGroup.reset()<\/code>, add or remove controls and validators, and more. The <a href=\"https:\/\/angular.dev\/api\/forms\/FormGroup?tab=api\" target=\"_blank\" rel=\"noopener\" title=\"\">full API is here<\/a>. That&#8217;s the power of <code>FormGroups<\/code> in action!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, during an Angular Accelerator coaching call, my client asked what is the point of using FormGroup. I then realized I had never written about FormGroup, so here is a post to fix that. When using reactive forms, we always have at least one FormGroup, which happens to be the entire form. This doesn&#8217;t mean [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,22],"tags":[],"class_list":["post-2126","post","type-post","status-publish","format-standard","hentry","category-angular","category-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=2126"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2126\/revisions"}],"predecessor-version":[{"id":2138,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2126\/revisions\/2138"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}