{"id":1276,"date":"2023-08-03T05:00:00","date_gmt":"2023-08-03T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1276"},"modified":"2023-08-02T15:31:23","modified_gmt":"2023-08-02T22:31:23","slug":"how-to-create-custom-form-controls-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-create-custom-form-controls-with-angular\/","title":{"rendered":"How to create custom form controls with Angular?"},"content":{"rendered":"\n<p>When working with lots of forms, you&#8217;ll likely use similar controls repeatedly: A country selection dropdown, a state selection dropdown, a date-range picker with specific constraints, etc. <\/p>\n\n\n\n<p>Instead of duplicating our code, which is never a good idea, Angular allows us to create custom form controls that integrate with <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/basic-form-validation-with-angular\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular&#8217;s form validation<\/a> mechanism.<\/p>\n\n\n\n<p>This feature requires our custom control component to implement an interface called <code><a href=\"https:\/\/angular.io\/api\/forms\/ControlValueAccessor\" target=\"_blank\" rel=\"noopener\" title=\"\">ControlValueAccessor<\/a><\/code> that has the following methods:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"234\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image.png\" alt=\"\" class=\"wp-image-1277\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image.png 527w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-300x133.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>I have a <a href=\"https:\/\/blog.angulartraining.com\/tutorial-custom-form-controls-with-angular-22fc31c8c4cc\" target=\"_blank\" rel=\"noopener\" title=\"\">detailed tutorial<\/a> and <a href=\"https:\/\/stackblitz.com\/edit\/ng-custom-form-control?file=src%2Fapp%2Fstate-selector.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">code example<\/a> if you want to dive deeper into that topic. <\/p>\n\n\n\n<p>It&#8217;s also worth noting that you don&#8217;t have to start from scratch when implementing a control value accessor. Angular has a <a href=\"https:\/\/angular.io\/api\/forms\/DefaultValueAccessor\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>DefaultValueAccessor<\/code> directive<\/a> (the one used by <code>FormControl<\/code>, <code>NgModel<\/code>, etc., in Angular forms) that can be invoked with the selector <code>ngDefaultControl<\/code>, and there are a <a href=\"https:\/\/angular.io\/api\/forms\/ControlValueAccessor#class-implementations\" target=\"_blank\" rel=\"noopener\" title=\"\">few more options available<\/a>, such as the <code><a href=\"https:\/\/angular.io\/api\/forms\/SelectControlValueAccessor\" target=\"_blank\" rel=\"noopener\" title=\"\">SelectControlValueAccessor<\/a><\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with lots of forms, you&#8217;ll likely use similar controls repeatedly: A country selection dropdown, a state selection dropdown, a date-range picker with specific constraints, etc. Instead of duplicating our code, which is never a good idea, Angular allows us to create custom form controls that integrate with Angular&#8217;s form validation mechanism. This feature [&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,8,22],"tags":[],"class_list":["post-1276","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1276","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=1276"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1276\/revisions"}],"predecessor-version":[{"id":1279,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1276\/revisions\/1279"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}