I created a custom Angular directive called myRequiredDirective
. I want to conditionally apply it to an input control similar to how it can be done with [required]
:
<input class="form-control" [required]="false" />
However, when I try to do something similar with my myRequiredDirective
, I get an error: Can't bind to 'myRequiredDirective' since it isn't a known property of 'input'.
directive.ts
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Validator, AbstractControl, Validators, NG_VALIDATORS, ValidatorFn } from '@angular/forms';
@Directive({
selector: '[myRequiredDirective]',
providers: [{ provide: NG_VALIDATORS, useExisting: MyRequiredDirective, multi: true }]
})
export class MyRequiredDirective implements Validator {
private valFn = CustomValidator();
validate(control: AbstractControl): { [key: string]: any } {
return this.valFn(control);
}
}
function CustomValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
let isValid = true;
if (!control.value) {
isValid = false;
}
if (control.value == 0) {
// Set control invalid if value equals zero. Mainly used for drop downs where the default value is usually 0.
isValid = false;
}
return isValid ? null : { 'required': 'message will come from validators.component.html' };
};
}
Is there a way I can make my custom directive behave similar to [required]
?
Edit #1
I solved the Can't bind to 'myRequiredDirective' since it isn't a known property of 'input'.
by doing the following:
export class MyRequiredDirective implements Validator {
@Input() myRequiredDirective: boolean;
private valFn = CustomValidator(this.myRequiredDirective); // this.myRequiredDirective is undefined
validate(control: AbstractControl): { [key: string]: any } {
return this.valFn(control);
}
}
and in the HTML template:
<input class="form-control" [myRequiredDirective]="false" />
However, this.myRequiredDirective
is undefined when I try to pass it into the CustomValidator.