Hi
I want that Dob should be greater than doj . and minimum difference between the two should be 1 year.
ON below line i am getting error - Property 'dob' does not exist on type 'EmpAddEditComponent'
---------------TYPESCRIPT
export class EmpAddEditComponent implements OnInit {
empForm : FormGroup;
constructor(private _fb : FormBuilder){
this.empForm = this._fb.group({
dob:'',
doj:'',
})
}
}
*************** DIRECTIVE
import { Directive, Input, forwardRef } from '@angular/core';
import { NG_VALIDATORS, Validator, AbstractControl, ValidationErrors } from '@angular/forms';
@Directive({
selector: '[appDateRangeValidator]',
standalone: true,
providers: [{ provide: NG_VALIDATORS, useExisting: DateRangeValidatorDirective, multi: true }]
})
export class DateRangeValidatorDirective implements Validator {
@Input('appDateCompare') compareDate: Date;
validate(control: AbstractControl): ValidationErrors | null {
const currentDate = new Date(control.value);
if (this.compareDate && currentDate) {
if (currentDate < this.compareDate) {
return { 'dateCompare': true }; // Validation failed
}
}
return null; // Validation passed
}
}
Jayraj ChhayaPosted Jun 28, 2024, 7:16 AM
The error message "Property 'dob' does not exist on type 'EmpAddEditComponent'" indicates that the property 'dob' is not defined in the 'EmpAddEditComponent' class. To resolve this issue, you need to define the 'dob' property in the 'EmpAddEditComponent' class.
In your TypeScript code, add the 'dob' property to the 'empForm' FormGroup initialization as shown below:
By adding the 'dob' property to the 'EmpAddEditComponent' class and initializing it as a FormControl, you will be able to access the 'dob' property in the template without any errors.
Additionally, make sure that you have imported the necessary modules and components correctly in your Angular module file.