Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - assign custom validator to a FormGroup

Tags:

I need to assign a custom validator to a FormGroup. I can do this at the time the FormGroup is created like this:

let myForm : FormGroup;  myForm = this.formBuilder.group({         myControl1: defaultValue,         myControl2: defaultValue       }, { validator: this.comparisonValidator })  comparisonValidator(g: FormGroup) {       if (g.get('myControl1').value > g.get('myControl2'.value)) {         g.controls['myControl1'].setErrors({ 'value2GreaterThanValue1': true });         return;       } } 

I have a situation though where I need to add the custom validator after I've instantiated the FormGroup, so I'm trying to do this after instantiating myForm, instead of adding the validator when the form is instantiated:

let myForm : FormGroup;  myForm = this.formBuilder.group({         myControl1: defaultValue,         myControl2: defaultValue       })  this.myForm.validator = this.comparisonValidator; 

This gives me a compiler error:

Type '(g: FormGroup) => void' is not assignable to type 'ValidatorFn'.   Type 'void' is not assignable to type 'ValidationErrors'. 

How do I assign a validator to my FormGroup so that the formGroup is passed as the argument to my comparisonValidator function?

Update - I've added a line showing where I'm doing a setErrors in my comparisonValidator, to make it clearer exactly how I'm trying to set a validation error.

like image 507
Chris Halcrow Avatar asked Jun 29 '18 04:06

Chris Halcrow


People also ask

How do I add a custom validator to FormControl?

To validate required fields, for example, we can use the built-in required validator on template-driven forms by adding the required attribute. const control = new FormControl('', Validators. required); Those validators are very helpful because they allow us to perform standard form validation.

How can you add the validator function into the reactive forms?

A validator function returns true if the form field is valid according to the validator rules, or false otherwise. A validator can be plugged in directly into a reactive form simply by adding it to the validators list. Each form field has its own list of separate validators.

What is custom validator in Angular?

A validator in Angular is a function which returns null if a control is valid or an error object if it's invalid. For model-driven forms we create custom validation functions and pass them into the FormControl constructor.


2 Answers

I've created a stackblitz take a look.

In the component.ts file

import { Component } from '@angular/core'; import {FormBuilder,FormGroup, ValidationErrors, ValidatorFn} from '@angular/forms'  @Component({   selector: 'my-app',   templateUrl: './app.component.html',   styleUrls: [ './app.component.css' ] }) export class AppComponent  {   myForm: FormGroup;   defaultValue = 20;  constructor(private formBuilder: FormBuilder) {   this.myForm = this.formBuilder.group({         myControl1: this.defaultValue,         myControl2: this.defaultValue       });       debugger       this.myForm.setValidators(this.comparisonValidator()) }   public comparisonValidator() : ValidatorFn{        return (group: FormGroup): ValidationErrors => {           const control1 = group.controls['myControl1'];           const control2 = group.controls['myControl2'];           if (control1.value !== control2.value) {              control2.setErrors({notEquivalent: true});           } else {              control2.setErrors(null);           }           return;     };  } } 

In the component.html file

<div>   <form [formGroup]="myForm">     <input formControlName="myControl1" type="number">     <input formControlName="myControl2"  type="number">     <br>Errors: {{myForm.get('myControl2').errors | json}}   </form> </div> 
like image 148
Anuradha Gunasekara Avatar answered Sep 28 '22 16:09

Anuradha Gunasekara


For setting any validators (predefined or customs) after the instantiating the formGroup, you will need to use the setValiators() method of FormGroup. For Ex:

  let myFormGroup = this.    _fb.group({       control1: new FormControl('1', [])     });   myFormGroup.setValidators(this.customValidators());   customValidators(): ValidatorFn {    let myFun = (cc: FormGroup): ValidationErrors => {      if(cc.valid) return null;      else return {something: 'someError'};    };    return myFun;   } 

You can play here.

like image 39
Shadab Faiz Avatar answered Sep 28 '22 16:09

Shadab Faiz