{"id":550,"date":"2023-02-23T05:00:00","date_gmt":"2023-02-23T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=550"},"modified":"2023-02-22T16:35:05","modified_gmt":"2023-02-23T00:35:05","slug":"using-validation-functions-that-work-with-both-template-driven-and-reactive-forms","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/using-validation-functions-that-work-with-both-template-driven-and-reactive-forms\/","title":{"rendered":"Using validation functions that work with both template-driven and reactive forms"},"content":{"rendered":"\n<p>Yesterday, we looked at <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/custom-form-validation-functions\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to write a validation function<\/a> that works with reactive forms. Template-driven forms have a somewhat similar approach that also uses a validator function but requires a wrapper directive that implements the <code><a href=\"https:\/\/angular.io\/api\/forms\/Validator\" target=\"_blank\" rel=\"noopener\" title=\"\">Validator<\/a><\/code> interface like this one (<a href=\"https:\/\/angular.io\/guide\/form-validation#adding-custom-validators-to-template-driven-forms\" target=\"_blank\" rel=\"noopener\" title=\"\">example here<\/a>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"173\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-52.png\" alt=\"\" class=\"wp-image-552\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-52.png 711w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-52-300x73.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The easiest solution to write a validation function that works with both reactive forms and template-driven forms is to create a directive for the template-driven form validation and then expose the validation function as a static method of that class:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"265\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-53.png\" alt=\"\" class=\"wp-image-553\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-53.png 770w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-53-300x103.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-53-768x264.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>A static method has two advantages in that scenario:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It&#8217;s public<\/li>\n\n\n\n<li>It does not require an instance of the class (we can refer to it as <code>CreditCardValidator.validateCcNumber<\/code>)<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>As a result, we&#8217;d use our validation feature like this in a template-driven form:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"35\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-56.png\" alt=\"\" class=\"wp-image-557\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-56.png 784w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-56-300x13.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-56-768x34.png 768w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><\/figure>\n<\/div>\n\n\n<p>And like that in a reactive form:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"44\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-54.png\" alt=\"\" class=\"wp-image-555\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-54.png 892w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-54-300x15.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-54-768x38.png 768w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"38\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-55.png\" alt=\"\" class=\"wp-image-556\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-55.png 796w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-55-300x14.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-55-768x37.png 768w\" sizes=\"auto, (max-width: 796px) 100vw, 796px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can check the complete code for that <a href=\"https:\/\/stackblitz.com\/edit\/angular-custom-validator-form?file=app%2Fcreditcardvalidator.directive.ts,app%2Fapp.component.html,app%2Fapp.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">example on Stackblitz here<\/a>. Here is another <a href=\"https:\/\/blog.angulartraining.com\/how-to-implement-custom-form-validators-with-angular-861651b0dc2c\" target=\"_blank\" rel=\"noopener\" title=\"\">tutorial for more information on that validation approach<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we looked at how to write a validation function that works with reactive forms. Template-driven forms have a somewhat similar approach that also uses a validator function but requires a wrapper directive that implements the Validator interface like this one (example here): The easiest solution to write a validation function that works with both [&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-550","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\/550","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=550"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":560,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/550\/revisions\/560"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}