“Validação angular da data de início da data” Respostas de código

A partir da data e até a data de validação no Angular 8

export class CustomeDateValidators {
    static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
        return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
            const fromDate = formGroup.get(fromDateField).value;
            const toDate = formGroup.get(toDateField).value;
           // Ausing the fromDate and toDate are numbers. In not convert them first after null check
            if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
                return {[errorName]: true};
            }
            return null;
        };
    }
}

/*--- implementations ---*/
this.form = this.fb.group({
  fromDate: null,
  toDate: null,
}, { validator: [
  //Default error with this validator:  {fromToDate: true}
  CustomeDateValidators.fromToDate('fromDate', 'toDate')
  
  // For custome error name like: {customeErrorName: true}, pass third optional parameter with custome name
  // CustomeDateValidators.fromToDate('fromDate', 'toDate', 'customeErrorName')
]});
Scriper

Validação angular da data de início da data

export class CustomeDateValidators {
    static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
        return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
            const fromDate = formGroup.get(fromDateField).value;
            const toDate = formGroup.get(toDateField).value;
           // Ausing the fromDate and toDate are numbers. In not convert them first after null check
            if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
                return {[errorName]: true};
            }
            return null;
        };
    }
}

/*--- implementations ---*/
this.form = this.fb.group({
  fromDate: null,
  toDate: null,
}, { validator: [
  //Default error with this validator:  {fromToDate: true}
  CustomeDateValidators.fromToDate('fromDate', 'toDate')
  
  // For custome error name like: {customeErrorName: true}, pass third optional parameter with custome name
  // CustomeDateValidators.fromToDate('fromDate', 'toDate', 'customeErrorName')
]});

Scriper

Respostas semelhantes a “Validação angular da data de início da data”

Perguntas semelhantes a “Validação angular da data de início da data”

Mais respostas relacionadas para “Validação angular da data de início da data” em TypeScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código