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
Choose a date
MM/DD/YYYY
Choose a date
MM/DD/YYYY
Cancel
Save
---------------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 ChhayaRank #304 · 8.2K reputationPosted Jun 28, 2024, 3:16 AM EDT
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:
export class EmpAddEditComponent implements OnInit { empForm: FormGroup; dob: FormControl; // Add this line constructor(private _fb: FormBuilder) { this.dob = new FormControl(''); // Add this line this.empForm = this._fb.group({ dob: this.dob, // Update this line doj: '', }); } }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.