<?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: Bledar Ramo</title>
    <description>The latest articles on DEV Community by Bledar Ramo (@bledar_ramo).</description>
    <link>https://dev.to/bledar_ramo</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%2F1105650%2F4d8fc9ed-97b9-4a29-a0fc-73ef879623d4.png</url>
      <title>DEV Community: Bledar Ramo</title>
      <link>https://dev.to/bledar_ramo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bledar_ramo"/>
    <language>en</language>
    <item>
      <title>Angular Forms: Building Complex Reactive Forms with ngJoyValidators</title>
      <dc:creator>Bledar Ramo</dc:creator>
      <pubDate>Wed, 21 Jun 2023 12:45:14 +0000</pubDate>
      <link>https://dev.to/bledar_ramo/angular-forms-building-complex-reactive-forms-with-ngjoyvalidators-4nhk</link>
      <guid>https://dev.to/bledar_ramo/angular-forms-building-complex-reactive-forms-with-ngjoyvalidators-4nhk</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NUPJ9g5s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3j44tts9hnajjzme0dr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NUPJ9g5s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3j44tts9hnajjzme0dr.png" alt="angular form complexity" width="800" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Building forms with simple validation requirements is relatively straightforward in Angular. However, as the complexity of the validation rules increases, it becomes challenging to maintain clean and manageable code. Cross-field validation, where the value of one or more  field depends on the value of another field, and dynamic validation, where the validation rules change based on user interactions, add an extra layer of complexity.&lt;/p&gt;

&lt;p&gt;To address these challenges I created ngJoyValidators a super light on size but heavy on functionality and flexibility library, which provides a collection of powerful custom validators designed to simplify complex form validation scenarios. These validators can be easily integrated into your Angular projects, making it easier to handle cross-field validation, dynamic validation, conditionally add / remove / compare/ disable / co-require on or more validators based on a dependency value from another field or same fields&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning Outcomes
&lt;/h3&gt;

&lt;p&gt;By the end of this section, you will achieve the following learning outcomes:&lt;/p&gt;

&lt;p&gt;• Understand how to implement reactive forms  with  custom complex validation scenarios in Angular.&lt;br&gt;
• Learn how to work with cross-field validation and dynamic validation in Angular.&lt;br&gt;
• Gain knowledge of advanced techniques for handling large and complex forms with reactive forms in Angular.&lt;/p&gt;
&lt;h2&gt;
  
  
  PART 1 : Conditional Validators with ngJoyValidators
&lt;/h2&gt;

&lt;p&gt;In the productListingForm, we have leveraged the power of conditionalValidatorsto handle complex validation scenarios. By utilizing conditionalValidators, we can dynamically apply validators to form controls based on specific conditions. This allows us to create a flexible and robust form that adapts to user input.&lt;/p&gt;

&lt;p&gt;For example, the productDetails field is conditionally required based on the selected productCategory. Using conditionalValidators, we can specify that the productDetails field should be required if the product category is either 'electronics' or 'books'. This ensures that users provide additional details for these specific categories, while not enforcing the requirement for other categories.&lt;/p&gt;

&lt;p&gt;Additionally, we have implemented conditional validation for fields like reviewComments, promotionPrice, and shippingCost. Each of these fields has its own set of conditions that determine whether they should be required or have other validation rules applied. This level of flexibility allows us to create a dynamic form that adapts to different scenarios and user input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.productListingForm = this.fb.group({
  productName: ['', Validators.required],
  productPrice: ['', [Validators.required, Validators.min(1)]],
  productCategory: ['', Validators.required],
  productDetails: ['', this.conditionalValidators&amp;lt;string&amp;gt;('productCategory',
      [Validators.required],
      (productCategory) =&amp;gt; productCategory === 'electronics' || productCategory === 'books'
  )],
  productRating: [5, [Validators.min(1), Validators.max(5)]],
  reviewComments: ['', this.conditionalValidators&amp;lt;number&amp;gt;('productRating',
      [Validators.required],
      (productRating) =&amp;gt; productRating &amp;lt; 3
  )],
  hasPromotion: [false],
  promotionPrice: ['', this.conditionalValidators&amp;lt;boolean&amp;gt;('hasPromotion',
      [Validators.required, Validators.min(1)],
      (hasPromotion, formControl) =&amp;gt; hasPromotion === true &amp;amp;&amp;amp; formControl.value &amp;lt; formControl.parent?.get('productPrice')?.value
  )],
  hasShipping: [true],
  shippingCost: ['', this.conditionalValidators&amp;lt;boolean&amp;gt;('hasShipping',
      [Validators.required, Validators.min(0)],
      (hasShipping, shippingControl) =&amp;gt; hasShipping === true &amp;amp;&amp;amp; ((shippingControl.value &amp;gt; 0 &amp;amp;&amp;amp; shippingControl.value &amp;lt; formControl.parent?.get('productPrice')?.value * 0.1) || shippingControl.value === 0)
  )]
});

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

&lt;/div&gt;



&lt;p&gt;This approach significantly reduces the amount of boilerplate code required for conditional validation, making the form implementation more maintainable and scalable &lt;/p&gt;

&lt;h4&gt;
  
  
  conditionalRequiredValidator and coRequiredValidator
&lt;/h4&gt;

&lt;p&gt;The conditionalRequiredValidator and coRequiredValidator are two useful validators provided by the ngJoyValidators library for handling conditional validation scenarios in Angular reactive forms.&lt;/p&gt;

&lt;p&gt;The conditionalRequiredValidator allows you to conditionally make a form control required based on a specific condition. For example, if you have a form field that should only be required when another field meets a certain condition, you can use the conditionalRequiredValidator to dynamically apply the required validation. It simplifies the process of making a field mandatory based on the state of other form controls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.demoForm = this.fb.group({    
  email: ['',[this.ngJoyDynamicValidatorsService.coRequiredValidator('email', 'phone', 'Either field1 or field2 is required.')]],    
  phone: ['',[this.ngJoyDynamicValidatorsService.coRequiredValidator('email', 'phone', 'Either field1 or field2 is required.')]]
});  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.sampleForm = this.fb.group({ 
  selectField: ['defaultOption'],  
  dependentField: [null, this.conditionalRequiredValidator( 'selectField', control =&amp;gt; control.value === 'specificOption','This field is required when specific option is selected in the dropdown.' )] 
});  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Part2: FormArray custom validators
&lt;/h2&gt;

&lt;p&gt;Ensure that FormArray instance in Angular form meet the desired range  length criteria.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aliases: this.fb.array([    
   this.fb.control('')
   ], {validators: [this.ngJoyValidatorsService.minMaxArrayLength(3, 7, "Min 3 aliases", "Max 7 aliases")]
})  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The **allFieldsFilled **validator plays a crucial role in ensuring that all controls within a FormArray are filled and valid, which is particularly useful for dynamic forms where fields can be dynamically added or removed. By applying this validator to a FormArray, it iterates through each control in the array and checks if any control fails validation. If any invalid control is found, it returns an error object indicating validation failure. However, if all controls are valid, it returns null, indicating successful validation. This validator is instrumental in enforcing the requirement of filling all fields in a dynamic form before submission.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aliases: this.fb.array([    
   this.fb.control('')
  ],
    {validators: [this.ngJoyValidatorsService.allFieldsFilled()]
 })  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The **unique **validator serves a crucial purpose by checking whether the value of a specific field within a form array is unique among its sibling controls. It follows a step-by-step process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.aliases.push(this.fb.control('',[this.ngJoyValidatorsService.unique()]));  

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

&lt;/div&gt;



&lt;p&gt;Overall, the unique function guarantees that the value entered into a control within a form array remains distinct from the values of its sibling controls, preventing duplicates and ensuring data integrity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3: Miscellaneous Validators
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Compare Password&lt;/strong&gt;:Check whether the value of two formControls are same or not .Below  is an example of comparing password but is can used for any controller type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; password: ['', [this.ngJoyValidatorsService.password({    
  validation: {    
  minLength: 5,    
  maxLength: 10,    
  digit: true,    
  specialCharacter: true    
  },message: 'Password must have at least one digit, one special character, and be between 5 and 10 characters in length.'
   }), 
  Validators.required]],
 repeatPass: ['', [this.ngJoyValidatorsService.compare('password'), Validators.required]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sanitize&lt;/strong&gt;: Sanitize input validator prevents the inclusion of harmful scripts or elements that could compromise the security of your web application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.validationService.sanitizeInput('Input must not contain scripting.') ]],   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Script Language Type&lt;/strong&gt;: Check if the input text of a control follows a specific script or writing system (e.g., Latin, Cyrillic, Arabic).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; scriptExample: ['', [    
  this.validationService.scriptLanguageType('Latin', 'Input must only contain Latin script characters.') 
]], 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to the validators mentioned above, the ngJoyValidators library offers a comprehensive collection of over 30 additional custom validators. These validators cover a wide range of use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 4: Credit Card / Bank Validators
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Credit Card&lt;/strong&gt;: validates if the entered credit card number is correctly formatted and matches a specific card type using algorithms such as Luhn's algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CVV&lt;/strong&gt;: ensures that the provided CVV is a valid 3-digit or 4-digit number based on the card type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expiry Date&lt;/strong&gt;: checks whether the given credit card expiration date has already passed or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.paymentForm = this.fb.group({    
  cardNumber: ['', [this.ngJoyValidators.creditCard('Please enter a valid card number.')]],    
  expiryDate: ['', [this.ngJoyValidators.expiryDate('Card has expired.')]],    
  cvv: ['', [this.ngJoyValidators.cvv('Invalid CVV.')]] 
});  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;IBAN Validator&lt;/strong&gt;: the International Bank Account Number format to ensure the correctness of bank account numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; this.paymentForm = this.fb.group({     
  iban: ["",[this.ngJoyValidators.ibanValidator()]]  
}); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--asr3rfZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0gfvdixpk80d2aowyry.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--asr3rfZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0gfvdixpk80d2aowyry.jpg" alt="INTERESTED FOR MORE INFO" width="290" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interested&lt;/strong&gt; : in exploring full list of available custom validators , more code examples, demos, ? Check out the ngJoyValidators library at &lt;a href="https://www.npmjs.com/package/@ngjoy.dev/reactivevalidators"&gt;https://www.npmjs.com/package/@ngjoy.dev/reactivevalidators&lt;/a&gt; for a wide range of powerful validators that can enhance your form-building experience , simplify your form validation logic, solve complexity, and deliver a seamless user experience&lt;/p&gt;

</description>
      <category>angular</category>
      <category>reactive</category>
      <category>validators</category>
      <category>form</category>
    </item>
  </channel>
</rss>
