Hi
How to put validations. I am using Reactive form
employeeArray: any[] = [];
objEmployee: any = {
"empId":0,
"empName": "",
"empContactNo": "",
"empEmail": "",
"addressLine1": "",
"addressLine2": "",
"pinCode":"",
"city": "",
"state": "",
}
constructor(private empSrv:EmployeeService){}
form = new FormGroup({
empName: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
empContactNo: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
email: new FormControl('', [Validators.required, Validators.email]),
addressLine1: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
addressLine2: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
pinCode: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
city: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),]),
state: new FormControl('', [Validators.required, Validators.minLength(3),Validators.maxLength(50),])
});
get f(){
return this.form.controls;
}
Thanks
Jayraj ChhayaPosted May 21, 2024, 7:20 AM
To add validations to your Angular Reactive Form in a modal, you can use the built-in Validators class provided by Angular. Here's how you can implement validations in your code:
formControlNamedirective:empNamewith the respective form control name.In your component class, you can access the form controls and their validation status using the
form.controlsproperty:Now, when a user interacts with the form, the validations will be triggered and the corresponding error messages will be displayed if the input is invalid.
Remember to import the necessary modules and classes, and make sure to include the
ReactiveFormsModulein your app module.