{"id":7081,"date":"2023-04-12T12:21:09","date_gmt":"2023-04-12T10:21:09","guid":{"rendered":"http:\/\/blog.bart.sk\/en\/?p=7081"},"modified":"2024-01-25T14:01:19","modified_gmt":"2024-01-25T13:01:19","slug":"types-angular-forms-fits","status":"publish","type":"post","link":"https:\/\/blog.bart.sk\/en\/types-angular-forms-fits\/","title":{"rendered":"Types of Angular forms: Which fits where?"},"content":{"rendered":"<p><b>Forms are both the foundation and the most vulnerable part of websites. In a series of blogs about security, we&#8217;ve often written about how and why all inputs need to be treated, and that this is the most common way that cyber criminals use to hack into a web. Therefore, when choosing how to encode a form, you need to be careful. Fortunately, with Angular, for example, we have several options.<\/b><\/p>\n<h2><b>Template forms<\/b><\/h2>\n<p><b>This is the simplest type defined in a template, in which ngModel is used to bind data to the form.<\/b><span style=\"font-weight: 400;\"> The code of such a form with name, surname, phone number and e-mail fields looks like this:<\/span><\/p>\n<pre>&lt;form (ngSubmit)=\"onSubmit()\" #myForm=\"ngForm\"&gt;\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"name\"&gt;Meno:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"text\" id=\"name\" name=\"name\" ngModel required&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"surname\"&gt;Priezvisko:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"text\" id=\"surname\" name=\"surname\" ngModel required&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"phone\"&gt;Telef\u00f3nne \u010d\u00edslo:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"tel\" id=\"phone\" name=\"phone\" ngModel required&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"email\"&gt;Email:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"email\" id=\"email\" name=\"email\" ngModel required&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;button type=\"submit\" [disabled]=\"!myForm.form.valid\"&gt;Odosla\u0165&lt;\/button&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/form&gt;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">At first glance, everything looks fine. However, the second and third glance may reveal that the<\/span><b> form doesn&#8217;t check the input format, for example, whether there really are numbers in the phone number and whether the e-mail has an at sign<\/b><span style=\"font-weight: 400;\">. Therefore it&#8217;ll be sent with any error, no problem. These forms are thus suitable only for really small projects of a personal nature and I wouldn&#8217;t let them out into the world.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If you want to use them anyway, I recommend predefined\u00a0 (<\/span><a href=\"https:\/\/angular.io\/api\/forms\/Validators\"><span style=\"font-weight: 400;\">https:\/\/angular.io\/api\/forms\/Validators<\/span><\/a><span style=\"font-weight: 400;\">) or even custom (<\/span><a href=\"https:\/\/angular.io\/guide\/form-validation\"><span style=\"font-weight: 400;\">https:\/\/angular.io\/guide\/form-validation<\/span><\/a><span style=\"font-weight: 400;\">) <\/span><b>validators that treat the security of the forms<\/b><span style=\"font-weight: 400;\">. They&#8217;re available to programmers in the form of templates and can be deployed on both type and reactive forms.\u00a0<\/span><\/p>\n<h2><b>Reactive forms<\/b><\/h2>\n<p><b>With this type, FormControl and FormGroup are used to bind data to the form.<\/b><span style=\"font-weight: 400;\"> The result looks something like this:<\/span><\/p>\n<h3><b>HTML Code:<\/b><\/h3>\n<pre><span style=\"font-weight: 400;\">&lt;form [formGroup]=\"myForm\" (ngSubmit)=\"onSubmit()\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"name\"&gt;Meno:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"text\" id=\"name\" formControlName=\"name\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"surname\"&gt;Priezvisko:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"text\" id=\"surname\" formControlName=\"surname\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"phone\"&gt;Telef\u00f3nne \u010d\u00edslo:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"tel\" id=\"phone\" formControlName=\"phone\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;label for=\"email\"&gt;Email:&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;input type=\"email\" id=\"email\" formControlName=\"email\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0&lt;button type=\"submit\" [disabled]=\"!myForm.valid\"&gt;Odosla\u0165&lt;\/button&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/form&gt;<\/span><\/pre>\n<h3><b>Component code:<\/b><\/h3>\n<pre><span style=\"font-weight: 400;\">import { Component, OnInit } from '@angular\/core';<\/span>\r\n<span style=\"font-weight: 400;\">import { FormGroup, FormBuilder, Validators } from '@angular\/forms';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">@Component({<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0selector: 'app-form',<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0templateUrl: '.\/form.component.html',<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0styleUrls: ['.\/form.component.css']<\/span>\r\n<span style=\"font-weight: 400;\">})\r\n<\/span>\r\n<span style=\"font-weight: 400;\">export class FormComponent implements OnInit {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0myForm: FormGroup;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0constructor(private formBuilder: FormBuilder) { }\r\n<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0ngOnInit(): void {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0this.myForm = this.formBuilder.group({<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0name: ['', Validators.required],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0surname: ['', Validators.required],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0phone: ['', Validators.required],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0email: ['', [Validators.required, Validators.email]]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0});<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0onSubmit() {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0console.log(this.myForm.value);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><b>Here, some validators are incorporated into the code, so the user can&#8217;t send just about anything.<\/b><span style=\"font-weight: 400;\"> For large projects, at least such basic type control is almost a necessity. Thanks to it, programmers know at any moment what type of data will arrive in the system via the field in the form.\u00a0<\/span><\/p>\n<p><b>Various mechanics could also be used to extract data types from a regular template form too<\/b><span style=\"font-weight: 400;\">. But why do the extra work when we can reach for the reactive forms directly in the design, and do it properly and more effectively? ?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Make the code a little bit better and it would be ready to go out into the world. We could end at this point, but <\/span><b>since version 14, Angular has prepared one interesting novelty for us<\/b><span style=\"font-weight: 400;\">, which is worth continuing.<\/span><\/p>\n<h2><b>Reactive type forms<\/b><\/h2>\n<p><b>So far, when extracting content from a form, &#8220;any&#8221; type has been assigned to the content.<\/b><span style=\"font-weight: 400;\"> However, this doesn&#8217;t tell the programmer whether the new content will be a string, a boolean (yes\/no), or a number. For example, if they want to feed additional logic into the form or work with the data further, they&#8217;d definitely lack this information.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, reactive type forms make it possible to define them. In addition, <\/span><b>even the IDE itself, in which the programmer writes the code, warns the developer immediately upon an unauthorized pairing.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Thus, the complementary code to the HTML from the previous reactive form looks something like this:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">export class <\/span><span style=\"font-weight: 400;\">FormComponent <\/span><span style=\"font-weight: 400;\">implements <\/span><span style=\"font-weight: 400;\">OnInit {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">myForm<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">FormGroup<\/span><span style=\"font-weight: 400;\">&lt;{<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\">: FormControl&lt;<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\">&gt;<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">surname<\/span><span style=\"font-weight: 400;\">: FormControl&lt;<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\">&gt;<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">phone<\/span><span style=\"font-weight: 400;\">: FormControl&lt;<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\">&gt;<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">email<\/span><span style=\"font-weight: 400;\">: FormControl&lt;<\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\">&gt;<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">}&gt;<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">constructor<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">private <\/span><span style=\"font-weight: 400;\">formBuilder<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">FormBuilder<\/span><span style=\"font-weight: 400;\">) {}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">ngOnInit<\/span><span style=\"font-weight: 400;\">(): <\/span><span style=\"font-weight: 400;\">void <\/span><span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">myForm <\/span><span style=\"font-weight: 400;\">= <\/span><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">formBuilder<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">group<\/span><span style=\"font-weight: 400;\">({<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">surname<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">phone<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">email<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">email<\/span><\/i><span style=\"font-weight: 400;\">]]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0})<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">onSubmit<\/span><span style=\"font-weight: 400;\">() {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">myForm<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">value<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Alternatively, you can use a descent where the return value for &#8220;.group&#8221; also sets the type for &#8220;myForm&#8221;, and improve it a bit:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">export class <\/span><span style=\"font-weight: 400;\">FormComponent <\/span><span style=\"font-weight: 400;\">{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">myForm <\/span><span style=\"font-weight: 400;\">= <\/span><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">formBuilder<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">group<\/span><span style=\"font-weight: 400;\">({<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">surname<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">phone<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">email<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">[<\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">required<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Validators<\/span><span style=\"font-weight: 400;\">.<\/span><i><span style=\"font-weight: 400;\">email<\/span><\/i><span style=\"font-weight: 400;\">]]<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0})<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">constructor<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">private <\/span><span style=\"font-weight: 400;\">formBuilder<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">FormBuilder<\/span><span style=\"font-weight: 400;\">) {}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">onSubmit<\/span><span style=\"font-weight: 400;\">() {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">myForm<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">value<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">As I mentioned, <\/span><b>in such a case, IDE also performs a check over the code, which can come with useful errors such as:\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Or, in the case below, the string tries to paste into surnameCount, which is a number, however, because the programmer wanted to use it to inform about the length of the surname.<\/span><\/p>\n<p><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/04\/chyba-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-10759 aligncenter\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/04\/chyba-1.png\" alt=\"\" width=\"677\" height=\"336\" \/><\/a><\/p>\n<p><b>The error is corrected by &#8220;length&#8221;, which is the correct type for this value.<\/b><\/p>\n<p><a href=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/04\/chyba-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10760\" src=\"https:\/\/blog.bart.sk\/wp-content\/uploads\/2023\/04\/chyba-2.png\" alt=\"\" width=\"677\" height=\"336\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Reactive type forms are especially useful for programmers because they allow them to work strictly with the types they require<\/span><b>. Therefore, it cannot happen that random undefined values enter the application and the project gets into an unpredictable, even dangerous state.<\/b><span style=\"font-weight: 400;\"> Deploying types in forms can detect huge errors, as most sites on the web use forms.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It&#8217;s also easier for programmers to code. <\/span><b>Type checks won&#8217;t allow them to throw a string somewhere where a number should go, and cause a bigger bug at a later stage.<\/b><span style=\"font-weight: 400;\"> The transpiler itself and the IDE will warn them, &#8220;Hello, you won&#8217;t pass through here and won&#8217;t start the web because you have a wrong format or you&#8217;re doing an unauthorized operation!&#8221;.\u00a0<\/span><\/p>\n<p><b>Thanks to reactive type forms, we can therefore code &#8220;cleaner&#8221; and safer again.<\/b><span style=\"font-weight: 400;\"> Thank you, Angular 14+!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"Forms are both the foundation and the most vulnerable part of websites. In a series of blogs about&hellip;","protected":false},"author":17,"featured_media":7092,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":""},"categories":[199],"tags":[146,312,407,406],"_links":{"self":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/7081"}],"collection":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/comments?post=7081"}],"version-history":[{"count":1,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/7081\/revisions"}],"predecessor-version":[{"id":7082,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/posts\/7081\/revisions\/7082"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media\/7092"}],"wp:attachment":[{"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/media?parent=7081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/categories?post=7081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bart.sk\/en\/wp-json\/wp\/v2\/tags?post=7081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}