Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.2K
Vistas
Angular: mat-error is not working on custom validator

I am unable to properly implement mat-error style change when DatePicker end date input is less than the start date input. The button is able to recognize that the input is invalid by disabling itself, but no error style changes happens on 'end_date'.

Code:

    <mat-error *ngIf="userAddressValidations.hasError('validator')">
      End date must be greater than start date.
    </mat-error>

My goal is to show this error message when the custom validator detects the error.

This is my full sample code:

Stackblitz

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A mat-error only work for error in controls, not for errors in formGroup.

But before change your dateCheck to return an object with property "validator", not "date"

function dateCheck(): ValidatorFn {
  return (control: AbstractControl): { [key: string]: boolean } | null => {
    if (
      control.value.end_date &&  //<--check only if has value, not !==undefined
      (isNaN(control.value.end_date) ||
        control.value.end_date < control.value.start_date)
    ) {
      return { validator: true };  //<--see that it's "validator"
    }
    return null;
  };
}

Then create a customErrorMatcher

export class DateEndMatcher implements ErrorStateMatcher {
  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    const isSubmitted = form && form.submitted;
    //see the comparision
    return !!(control && (control.invalid || form.invalid) && (control.dirty || control.touched || isSubmitted));
  }
}

And declare in your .ts

endDateMatcher = new DateEndMatcher();

And use in the .html

 <input matInput [matDatepicker]="picker2" formControlName='end_date' 
        [errorStateMatcher]="endDateMatcher">

You can see in your forked stackblitz

NOTE: In the new material angular you has a range date picker

Update there' another approach that it's use SetErrors

function dateCheck(): ValidatorFn {
  return (control: AbstractControl): { [key: string]: boolean } | null => {
    if (
      control.value.end_date &&
      (isNaN(control.value.end_date) ||
        control.value.end_date < control.value.start_date)
    ) {
      //indicate that setErrors
      control.get('end_date').setErrors({validator:true})
      return { validator: true };
    }
    if (control.value.end_date)
       control.get('end_date').setErrors(null) //<--put the setErrors to null
    return null;
  };
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda