I am trying to make a custom validation so i have created the following directive:
import {Directive, Input} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, Validator} from '@angular/forms';
@Directive({
selector: '[appEqualValidator]',
providers: [
{provide: NG_VALIDATORS, useExisting: EqualValidatorDirective, multi: true}
]
})
export class EqualValidatorDirective implements Validator {
@Input('inputOne')
inputOne: string;
@Input('inputTwo')
inputTwo: string;
constructor() {
}
validate(c: AbstractControl) {
const value = c.value;
if ((value == null || value === undefined || value === '') && this.inputOne === this.inputTwo) {
return {
equalTo: {condition: this.inputOne === this.inputTwo}
};
}
return null;
}
}
Now I am trying to add this to my form however I am having some issues using the multiple inputs:
<div class="form-group">
<label>E-mail</label>
<input type="email" class="form-control" placeholder="Indtast email" [(ngModel)]="model.email" name="email" [email]="true" required/>
</div>
<div class="form-group">
<label style="display: block">Gentag E-mail</label>
<input type="email" class="form-control" placeholder="Gentag email" [(ngModel)]="model.emailRepeat" required [email]="true"
[appEqualValidator]="value" name="emailRepeat"/>
<li *ngIf="!emailVerify">
De to emails er ikke ens
</li>
</div>
I am not sure how to use the directive within the form. and making sure i can get both inputs.
I was hoping some of you could point me in the right direction.