Skip to main content
Rollback to Revision 1 - Edit approval overridden by post owner or moderator
Source Link
SiddAjmera
  • 39.6k
  • 7
  • 77
  • 113

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 
import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 
<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 
import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 
<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.

Source Link
SiddAjmera
  • 39.6k
  • 7
  • 77
  • 113

You can do something like this:

import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; @Component({...}) export class AppComponent { form: FormGroup = new FormGroup({ control: new FormControl(null, Validators.required) }); get validator() { const validator = this.form.get('control').validator({} as AbstractControl); console.log(validator); if (validator && validator.required) { return true; } } } 

And then in your Template:

<form [formGroup]="form" (submit)="onSubmit()"> Control: <span *ngIf="validator">*</span> <input type="text" formControlName="control"> <button>Submit</button> </form> 

NOTE: Just get the form control as a type of AbstractControl using this this.form.get('control').validator({} as AbstractControl);

This will return an Object with the list of validators that are present on your FormControl. You can then check for the required key in the Object. If it exists and its value is true then you can be sure that a Required Validator is applied on the FormControl.


Here's a Working Sample StackBlitz for your ref.