Help Angular by taking a 1 minute survey!Go to surveyHome

RequiredValidator

A directive that adds the required validator to any controls marked with the required attribute. The directive is provided with the NG_VALIDATORS multi-provider list.

See also

NgModules

Selectors

  • :not([type=checkbox])[required][formControlName]
  • :not([type=checkbox])[required][formControl]
  • :not([type=checkbox])[required][ngModel]

Properties

Property Description
@Input()
required: boolean | string

Tracks changes to the required attribute bound to this directive.

Description

Adding a required validator using template-driven forms

<input name="fullName" ngModel required>
      
      <input name="fullName" ngModel required>
    

Methods

Method that validates whether the control is empty. Returns the validation result if enabled, otherwise null.

validate(control: AbstractControl): ValidationErrors | null
      
      validate(control: AbstractControl): ValidationErrors | null
    
Parameters
control AbstractControl
Returns

ValidationErrors | null

Registers a callback function to call when the validator inputs change.

registerOnValidatorChange(fn: () => void): void
      
      registerOnValidatorChange(fn: () => void): void
    
Parameters
fn () => void

The callback function

Returns

void