Using Custom validators in angular reactive form

Discussão em 'Angular' iniciado por PremKumar, Novembro 28, 2024.

  1. PremKumar

    PremKumar Guest

    I'm trying to use custom validator to compare if the end time is greater than the start time.

    code:

    function timeValidator(): ValidatorFn {
    return (control: AbstractControl): { [key: string]: boolean } | null => {
    if (control.value !== undefined && (isNaN(control.value) || control.get('fromTime').value > control.get('toTime').value)) {
    return { 'ageRange': true };
    }
    return null;
    };
    }


    From formgroup

    toTime: new FormControl(null, [Validators.required, timeValidator(this.fromTime,this.toTime)]),


    I'm getting a error once I run this like: Cannot read property 'value' of null on the line if (control.value !== undefined && (isNaN(control.value) || control.get('fromTime').value > control.get('toTime').value))

    I need some help in fixing this. Thank you

    Continue reading...

Compartilhe esta Página