Since using reactive forms with ngModel is deprecated in Angular 6 and my project depends heavily on using ngModel, I decided to replace my reactive forms with template driven forms.
Before I do that, I want to make sure that all the things possible with reactive forms are possible with template driven forms as well.
One of the things that I need, but couldn't find any information on, is using custom validator function IN THE CONTROLLER with template driven forms. All of the tutorials that I've checked are creating separate directives for the custom validator or doing somthing similar to that.
I however really need to be able to access the data in the controller in the validator function, because the validity of a field depends on other data and other properties in the view. Also for the particular use case I find this making more sense than creating a separate file for the validator.
With the reactive forms I could do something like this:
this.thisForm = new FormGroup({
smallerValue: new FormControl('', [
this.validateSuccessShortName.bind(this) //the validator function
]),
biggerNumber:new FormControl('', [
Validators.required
])
});
}
validateSuccessShortName(control: AbstractControl) {
if (parseInt(control.value,10) > parseInt(this.biggerNumber, 10)) {
return {value: control.value};
}
return null;
}
However I can't find a way to do this for template driven forms.
How can I do something like this in template-driven forms?
The reactive forms example in stackblitz:
https://stackblitz.com/edit/ionic-tqp9o1?embed=1&file=pages/home/home.ts