<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Klement Omeri</title>
    <description>The latest articles on DEV Community by Klement Omeri (@klementomeri).</description>
    <link>https://dev.to/klementomeri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F319322%2Fd4eddf56-ee49-44cd-956c-bdaa497a8169.jpg</url>
      <title>DEV Community: Klement Omeri</title>
      <link>https://dev.to/klementomeri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/klementomeri"/>
    <language>en</language>
    <item>
      <title>Better Passwords With Angular and Django</title>
      <dc:creator>Klement Omeri</dc:creator>
      <pubDate>Sat, 18 Apr 2020 15:06:32 +0000</pubDate>
      <link>https://dev.to/klementomeri/better-passwords-with-angular-and-django-1g1c</link>
      <guid>https://dev.to/klementomeri/better-passwords-with-angular-and-django-1g1c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cirHeYhi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12000/1%2Al05_KIL40_5tcPcHcvVyNg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cirHeYhi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12000/1%2Al05_KIL40_5tcPcHcvVyNg.jpeg" alt="Photo by [Philipp Katzenberger](https://unsplash.com/@fantasyflip?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/security?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though I describe myself as a Pythonista today I have realized that I have always been writing only for the front-end. I like Angular and the world of front-end development but when it comes to python💛💙 it is different for me.&lt;/p&gt;

&lt;p&gt;So I decided to show something related to backend and I ended up writing this piece which has to do with both front and back. (Looks like there is a hidden love for the front-end too)&lt;/p&gt;

&lt;p&gt;In this piece, I am going to show you how you can ask your users to choose more secure passwords with a good user experience. Let’s take a look at what the product of this article will be. Below GIF is taken from the library npm site.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9haGhcYp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1032/1%2A-zHmOyjfvRqN4WXR0H22Kw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9haGhcYp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1032/1%2A-zHmOyjfvRqN4WXR0H22Kw.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be using the excellent library &lt;a href="https://github.com/angular-material-extensions/password-strength"&gt;password-strength&lt;/a&gt; from material-extensions. Thanks to &lt;a href="https://github.com/AnthonyNahas"&gt;Antony Nahas&lt;/a&gt; and all the contributors.&lt;/p&gt;

&lt;p&gt;I have the &lt;a href="https://github.com/klement97/angular-password-strength"&gt;front&lt;/a&gt; and &lt;a href="https://github.com/klement97/django-password-strength"&gt;back&lt;/a&gt; repo here if you are a show me the code type.&lt;/p&gt;

&lt;p&gt;I am skipping the project creation stuff, docs already explain it better than me. I have those two project structures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Front (Angular)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fnLDX9ag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5120/1%2AaqpND_ABrVfjmCVRN0F0Pg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fnLDX9ag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5120/1%2AaqpND_ABrVfjmCVRN0F0Pg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Back (Django and Django REST)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YuBD8S8l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5120/1%2Ax_4NebciyK-Lz9_a_nUHyQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YuBD8S8l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5120/1%2Ax_4NebciyK-Lz9_a_nUHyQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Begin with Backend
&lt;/h1&gt;

&lt;p&gt;As always let me begin with the backend❤️ Let’s install the requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.django-rest-framework.org/"&gt;Django REST Framework&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://djoser.readthedocs.io/en/latest/getting_started.html"&gt;Djoser &lt;/a&gt;— Authentication library&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will be skipping the configuration, they are out of the scope of this article. Please ask me if you struggle with anything. I highly suggest checking the Djoser lib in detail, it is a really good one built by the sunscrapers.&lt;/p&gt;




&lt;p&gt;Now thanks to Djoser lib I have a working endpoint which I can create a user. Let’s run the server and hit it. &lt;a href="http://127.0.0.1:8000/auth/users/"&gt;http://127.0.0.1:8000/auth/users/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lSvq7Mil--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5052/1%2Alr8stI_nK_9D8bDus5ElgA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lSvq7Mil--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5052/1%2Alr8stI_nK_9D8bDus5ElgA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I have made a try to create a user but the password(test) is not filling out the requirements so I got those error messages. Let’s look at what type of requirements does Django has by default. A section from &lt;em&gt;settings.py&lt;/em&gt; below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;User Attribute Similarity *&lt;/em&gt;— Ensures the password is not similar to user attributes like email, username and first and last names&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimum Length Validator&lt;/strong&gt; — Ensures the password is at least 8 chars. This can be changed by adding an option below like so&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'django.contrib.auth.password_validation.MinimumLengthValidator',
        'OPTIONS': {
            'min_length': 8,
        }
    },
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;Common Password Validator *&lt;/em&gt;— Ensures the password is not inside a 20.000 of passwords length of a list. Comparing is case-insensitive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;Numeric Password Validator *&lt;/em&gt;— Ensures the password is not entirely numeric&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The above validators are the default ones and can be extended which is exactly what we are going to do. We are going to write some more validators ourselves.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Number Validator&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Upper Case Validator&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lower Case Validator&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Symbol Validator&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Above you see the &lt;em&gt;validators.py&lt;/em&gt; file, it is as simple as it gets. We check for our condition and raise a Validation Error if it is not met.&lt;/p&gt;

&lt;p&gt;Add new validators to settings.py:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let’s check out what does the signup form looks like now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G3tNp3or--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5044/1%2AqxqDz8MPjtp9GRY9NVHIHA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G3tNp3or--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/5044/1%2AqxqDz8MPjtp9GRY9NVHIHA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OK, now the backend is done and we are sure it is not going to accept weak passwords even if the frontend sends them. Let’s move to the front-end side.&lt;/p&gt;




&lt;h1&gt;
  
  
  Front-end
&lt;/h1&gt;

&lt;p&gt;So let’s begin with the front-end side of the work. We are going to create a signup form and then integrate the password-strength component from material-extensions inside it.&lt;/p&gt;

&lt;p&gt;Install the library, you can check out the versions to install the required one for your angular version. The latest is v.6.0.0 which is Angular v9 supported.&lt;/p&gt;

&lt;p&gt;Make sure you have angular material installed and configured the right way and you have BrowserAnimationsModule inside the imports of app.module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng add @angular/material  // if material is not already installed

ng add @angular-material-extensions/password-strength
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above command will automatically add MatPasswordStrengthModule inside the imports array of app.module.ts but I have a lazy-loaded auth module for the sign-up components so let's move the module inside auth.module.ts&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qu7MMMRP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3300/1%2AsrsTaBp-pY2VkzEenlOefA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qu7MMMRP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3300/1%2AsrsTaBp-pY2VkzEenlOefA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Build Sign up form
&lt;/h1&gt;

&lt;p&gt;So we have the following sign up form.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zbeJAdu3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2080/1%2AGm-EVL4r0PDb9zqZPBhKhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zbeJAdu3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2080/1%2AGm-EVL4r0PDb9zqZPBhKhw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have linked server errors to an ordered list just below the password field. If we try to sign up without a valid password we can see the server errors like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iBq8O9m2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2596/1%2Afd5ypthhxRU3hZO-dW9X7A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iBq8O9m2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2596/1%2Afd5ypthhxRU3hZO-dW9X7A.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have the server errors displayed to the user but come on, who wants to show errors like this? Let’s integrate the material design password strength component.&lt;/p&gt;

&lt;p&gt;After we have imported MatPasswordStrengthModule inside our module now we can use the following strength meter and strength info. Let’s give an identifier to our password input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;mat-form-field appearance="outline" class="full-width"&amp;gt;
  &amp;lt;mat-label&amp;gt;Password&amp;lt;/mat-label&amp;gt;
  &amp;lt;input **#password** type="password" matInput formControlName="password" /&amp;gt;
&amp;lt;/mat-form-field&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Below the password field add password strength meter and info.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
formControlName="password" /&amp;gt;
&amp;lt;/mat-form-field&amp;gt;

&amp;lt;div style="margin-top: 10px; margin-bottom: 10px; width: 100%;"&amp;gt;
  &amp;lt;mat-password-strength
          #strength
          [password]="password.value"
          [enableLengthRule]="true"
          [enableDigitRule]="true"
          [enableLowerCaseLetterRule]="true"
          [enableUpperCaseLetterRule]="true"
          [enableSpecialCharRule]="true"
  &amp;gt;
  &amp;lt;/mat-password-strength&amp;gt;
  &amp;lt;mat-password-strength-info [passwordComponent]="strength"&amp;gt;
  &amp;lt;/mat-password-strength-info&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see you can enable or disable rules as you wish. Now the form looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YSFBdYb2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2188/1%2AFv1VlgU4QmhB08aHCDHgkg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YSFBdYb2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2188/1%2AFv1VlgU4QmhB08aHCDHgkg.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have finished the basic integration. There are lots of customizations that can be done. Please visit &lt;a href="https://www.npmjs.com/package/@angular-material-extensions/password-strength#usage"&gt;here&lt;/a&gt; to see all of them. I am going to show one of them which is how to change messages displayed to the user.&lt;/p&gt;

&lt;p&gt;Inside mat-strength-info we can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;mat-password-strength-info
        [passwordComponent]="strength"
        [digitsCriteriaMsg]="'The password must contain at least 1 digit'"
        [minCharsCriteriaMsg]="'The password must contains at least 8 chars'"
        [specialCharsCriteriaMsg]="'The password must contain 1 special char like (!£$%^&amp;amp;*()@~?,/.)'"
&amp;gt;
&amp;lt;/mat-password-strength-info&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By doing so you can give any message you like to the strength info component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1jYMxx5o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2064/1%2AqVqtwr6Nrd4qdCDkPgD7qQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1jYMxx5o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2064/1%2AqVqtwr6Nrd4qdCDkPgD7qQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;We wanted to increase the security level of passwords. We have begun with the backend validation and used only this validation at first by showing to the user whatever error message backend sent. After that to give our users a better experience we have used the mat-password-strength library and integrated it into our sign up form. Of course, it was easier to show the error messages of the backend and just leave it like so. But we want the user experience to be the best it can be, especially on a signup form everything must be easier and password validation is not an exception. If you want your users to do something you want then you need to ask them the correct way.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;




&lt;h1&gt;
  
  
  External links
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://github.com/klement97/django-password-strength"&gt;Backend repo&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/klement97/angular-password-strength"&gt;Frontend repo&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/@angular-material-extensions/password-strength#usage"&gt;Material Password Strength Library&lt;/a&gt;&lt;br&gt;
&lt;a href="https://djoser.readthedocs.io/en/latest/introduction.html"&gt;Djoser Authentication Library&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>javascript</category>
      <category>angular</category>
      <category>django</category>
    </item>
    <item>
      <title>How to Handle Errors in Angular Reactive Forms With 3 Lines of Code</title>
      <dc:creator>Klement Omeri</dc:creator>
      <pubDate>Tue, 14 Apr 2020 16:16:53 +0000</pubDate>
      <link>https://dev.to/klementomeri/how-to-handle-errors-in-angular-reactive-forms-with-3-lines-of-code-3nan</link>
      <guid>https://dev.to/klementomeri/how-to-handle-errors-in-angular-reactive-forms-with-3-lines-of-code-3nan</guid>
      <description>&lt;p&gt;DRY up your forms&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F12000%2F1%2APk56erlIH51A1Oft5lJLOA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F12000%2F1%2APk56erlIH51A1Oft5lJLOA.jpeg" alt="Photo by [Emile Guillemot](https://unsplash.com/@emilegt?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/future?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Angular is a great framework that offers some great tools. One of these tools is &lt;code&gt;ReactiveForms&lt;/code&gt;. In this post, I want to introduce you to an easier way to handle errors on reactive forms and avoid spaghetti code. I’ll jump straight to the implementation to save time.&lt;/p&gt;

&lt;p&gt;What we’re going to build is a standard signup form, like the one shown below. I’ll be using Angular v9 and you can find the source code &lt;a href="https://github.com/klement97/reactive-forms-errorhandler" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AfLprX9rsXOqexgKaMuSZTQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AfLprX9rsXOqexgKaMuSZTQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, I have a very simple signup form here styled with angular material. The &lt;code&gt;ts&lt;/code&gt; code for the form is below — I’ve added some &lt;code&gt;Validators&lt;/code&gt; to get some errors:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I’ve seen so much code from different projects and one thing that hasn’t changed at all is error handling on forms. Let me show you what I mean:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For every single error that may occur you need to write another &lt;code&gt;mat-error&lt;/code&gt; tag, check if the error is present, and set an appropriate message for it. This has to be done repeatedly. When you have a single signup form that might not be an issue but if you have dozens of forms, you need to write the same code every time and it is &lt;em&gt;tiring&lt;/em&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Let’s Think About It
&lt;/h1&gt;

&lt;p&gt;We are programmers, not copy pasters. We must write beautiful and clean code. That’s why I decided to take a moment and think about it.&lt;/p&gt;

&lt;p&gt;I’m kidding. Of course, it’s true we must write beautiful and clean code but that wasn’t why I decided to think about what could be done better. In fact, the moment I decided something had to change was when my team leader assigned me to implement error handling on more than 20 forms with an average of ten fields each and many error cases. The errors must appear below each field that the error is related to and server errors must be handled the same way too.&lt;/p&gt;

&lt;p&gt;So, errors must appear like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2APgbvgZM7p2cdPG9t2hghwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2APgbvgZM7p2cdPG9t2hghwg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ve begun to work on some global functions and achieved some success with them — but they’re also coupling my code on each component. I wanted something decoupled, something that can be copied and pasted in any other project and work like a charm. So I ended up creating an injectable class and called it &lt;code&gt;ErrorHandler&lt;/code&gt;(very creative name).&lt;/p&gt;

&lt;p&gt;You can check out the class &lt;a href="https://gist.github.com/klement97/58ab606031c4fe4a2a04b6ed8c2e9d92" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It will take a long time to dive into what’s inside this class, so I’ll talk only about how you can easily use it, as I said, with only three lines of code. Anyway, I’ll be happy if anyone has any ideas to improve it — just contact me. My goal is to work on it a little more and transform it into an npm package.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Main Idea
&lt;/h1&gt;

&lt;p&gt;The idea behind this class is that for every form we have we also create an object of errors. We take all control names from the form and assign them as keys to error object and the error message of each form control assign as value to those keys.&lt;/p&gt;

&lt;p&gt;If that hasn’t made it clear enough I think the code below will:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;Take the code from my &lt;a href="https://gist.github.com/klement97/58ab606031c4fe4a2a04b6ed8c2e9d92" rel="noopener noreferrer"&gt;gist&lt;/a&gt; and create a file somewhere in your project and paste it. It doesn’t matter where the file is located. You only need Reactive Forms to be imported in your module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1500%2F1%2AnfbWhjDTxbhYbbdGuq6GFg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1500%2F1%2AnfbWhjDTxbhYbbdGuq6GFg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Import and inject it into your component’s constructor, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2788%2F1%2Aq0SELt_XTF4oni63r2lj5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2788%2F1%2Aq0SELt_XTF4oni63r2lj5w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create an empty object to hold errors inside your component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1460%2F1%2AzhGpScYbLqT5QeOjJ1Jrww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1460%2F1%2AzhGpScYbLqT5QeOjJ1Jrww.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Call &lt;code&gt;handleErrors()&lt;/code&gt; method from the class inside &lt;code&gt;onInit()&lt;/code&gt; but after you initialize your form:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2052%2F1%2ADUmbC9gjCQ2ZzVrNed4xGg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2052%2F1%2ADUmbC9gjCQ2ZzVrNed4xGg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;handleError()&lt;/code&gt; method takes two arguments — the first one is your form and the second one is a local empty object to keep errors.&lt;/p&gt;

&lt;p&gt;Now go to your template and write only a single mat-error tag like this for each &lt;code&gt;formControl&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2884%2F1%2AuxXzSRg9-an3xaFO0x90Hg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2884%2F1%2AuxXzSRg9-an3xaFO0x90Hg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, inside the &lt;code&gt;mat-error&lt;/code&gt;, this is the only thing you need to write:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;mat-error&amp;gt;{{errors.theFormControlName}}&amp;lt;/mat-error&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now you’re not writing repeated code all over the app and the errors are visible below the field that has the error — great!&lt;/p&gt;

&lt;p&gt;Also, there’s a method called &lt;code&gt;organizeServerErrors&lt;/code&gt; to deal with validation errors sent from the server, this method is written explicitly to work with the way that my backend with Django Rest sents me errors. So if you are going to use it you need to work around some to change in your backend’s error format.&lt;/p&gt;

&lt;p&gt;Anyway, it is enough to call &lt;code&gt;setErrors()&lt;/code&gt; on the required form control and add the error type into the error cases in the class, as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// where your error comes from the server
this.signUpForm.get('email').setErrors({emailInUse: true});

// error.handler.ts
...
} else if (errors.pattern) {
    this.message = 'Invalid value';
} else if (errors.passwordMismatch) {
    this.message = 'Passwords do not match';
} else if (errors.emailInUse) {
    this.message = 'There is an account with that email';
} else {
    this.message = '';
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;We’re all bored of writing the same things over and over again. This class offers a central solution to error handling in Angular ReactiveForms.&lt;/p&gt;

&lt;p&gt;Currently, I’m working on the implementation to handle the form arrays that hold other form groups inside them too. The goal is simple: to have one method call to handle all errors.&lt;/p&gt;

&lt;p&gt;If you want to work with me or give any suggestions on the class code I will be very happy to hear from you!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>html</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Why You Should Never Use Methods Inside Templates in Angular</title>
      <dc:creator>Klement Omeri</dc:creator>
      <pubDate>Mon, 13 Apr 2020 22:06:13 +0000</pubDate>
      <link>https://dev.to/klementomeri/why-you-should-never-use-methods-inside-templates-in-angular-2p3m</link>
      <guid>https://dev.to/klementomeri/why-you-should-never-use-methods-inside-templates-in-angular-2p3m</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F960%2F1%2AYQa7h43uWps6fHUV6u-DVA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F960%2F1%2AYQa7h43uWps6fHUV6u-DVA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Angular is a great framework that offers great tools for developers to build web applications easily. One of its core features is HTML is written into templates that are HTML files without any DOCTYPE declaration. They can start with any HTML tag you want because they;ll be attached to the &lt;code&gt;index.html&lt;/code&gt; file, which has elements like the DOCTYPE, metadata, links to scripts and styles, etc.&lt;/p&gt;

&lt;p&gt;The cool part about templates isn’t that they don’t require a DOCTYPE or metadata. The cool part is they can contain some things regular HTML files can’t. One of those things is you can open up double braces anywhere and include some TypeScript inside it. Just like that&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2204%2F1%2AtMgYE6grzEFUUCPP_89WWg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2204%2F1%2AtMgYE6grzEFUUCPP_89WWg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have the user object here, and we’re assigning its &lt;code&gt;image&lt;/code&gt;’s path to the &lt;code&gt;src&lt;/code&gt; attribute of the &lt;code&gt;img&lt;/code&gt; tag. That is a quite useful feature. Just like the &lt;code&gt;user&lt;/code&gt; variable, you can also use methods inside double braces.&lt;/p&gt;

&lt;p&gt;So one example can be this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2052%2F1%2A6wrPwwB21WM-0RM1LHOJLg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2052%2F1%2A6wrPwwB21WM-0RM1LHOJLg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here ,we use a helper method called &lt;code&gt;getAddress()&lt;/code&gt; to get the address from the user object in a representative way. The method is so basic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1536%2F1%2ATb1Dyiui0Kn3GUczDLtgRQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1536%2F1%2ATb1Dyiui0Kn3GUczDLtgRQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nothing wrong can happen, right?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F840%2F1%2ARHoBYQYFFbs8WY2eBJpwhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F840%2F1%2ARHoBYQYFFbs8WY2eBJpwhg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It looks like we have what we need. The address is right there.&lt;/p&gt;

&lt;p&gt;Do you want to know what’s wrong with it? Let’s just place a log at the beginning of the method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1528%2F1%2A1pfrZwubagQ75lt4MfyvhQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1528%2F1%2A1pfrZwubagQ75lt4MfyvhQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this way, we could know when this method has been triggered. What we are expecting now is to see this console only once, but will that happen?&lt;/p&gt;

&lt;p&gt;When I just refresh the page and open the console, I see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AIBu4j5zxOpPUCOxnaXYfLw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AIBu4j5zxOpPUCOxnaXYfLw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;getAddress()&lt;/code&gt; method has been triggered four times after the page refreshed. And every time I click on the page, hover the mouse over the text area, or click on it, I get more and more consoles.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AjMm_-N_a6W7gqEaI8EOfhQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AjMm_-N_a6W7gqEaI8EOfhQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This happens because of Angular change detection. I can assure you there are no problems with the change detection of Angular — the problem is on our side. We have used a method inside a template inside double braces. That’s not very clever. What we should do is assign this address representation to a variable and be sure to call this method only once.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1128%2F1%2AwD-3dJbjcnKz63YJX03dig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1128%2F1%2AwD-3dJbjcnKz63YJX03dig.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2204%2F1%2AZVk-tuLQ_8Y4izx4X5fuWQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2204%2F1%2AZVk-tuLQ_8Y4izx4X5fuWQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this way, the &lt;code&gt;getAddress()&lt;/code&gt; method will be called only once, and the string representation of the user’s address will be assigned to the &lt;code&gt;address&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;We can be sure the method is being called only once just by looking at the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AzfFcrMpPhTncbJKD4DdFsQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F5120%2F1%2AzfFcrMpPhTncbJKD4DdFsQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By triggering the &lt;code&gt;getAddress&lt;/code&gt; every time we click or hover in the text area, we just slow down our application.&lt;/p&gt;

&lt;p&gt;This may not be noticeable for such a simple case. but imagine having a large form filled with values using methods like that. The performance of that app would be affected much more.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;With frameworks like Angular, we get a lot of cool features regular HTML doesn’t give us.&lt;/p&gt;

&lt;p&gt;But if the power of those features is being used without thinking, then we start hearing our team talk about slow frameworks, how Angular is bad, etc. In fact, in most cases, the problem is inside our code — we just didn’t notice it.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>angular</category>
      <category>webdev</category>
      <category>html</category>
    </item>
  </channel>
</rss>
