Friday, November 3, 2023

Custom Validations in Angular -Part 10

 Developer writing code to perform validations to application specific is
  called "custom validations"
 Custom validations logic should be written with in static methods
 
Syntax:
     export class <validationsclassname>
     {
         static <methodname>(para:FormControl|FormGroup)
         {
              logic to perform validation
                 ...
              return({methodname:true}); //validation failed
                or
              return null;  //validation success
         }
          ...
     }


para with FormControl is required for validation based on single prop
para with FormGroup is  required for validation based on multiple props

Create a class validations.








import { FormControl, FormGroup } from "@angular/forms";

export class Validations {
    static isavailable(fc:FormControl)
    {//para fc will reference to username prop
        if(fc.value=="ramu" || fc.value=="raju")
        return({isavailable:true});
        else
        return null;
    }
    static comparepwds(fg:FormGroup)
    {
        if(fg.value.confirmpassword==fg.value.password)
        return null;
        else
        return({comparepwds:true});

    }
}

No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...