My form is prefilled with user.email
. I wish to only enable the edit button if the user fills the FormControl
's field with a value other than the original user.email
. Is it possible to do this with a custom Validator?
This is my validator:
import { FormControl } from '@angular/forms';
export class ChangedValidator {
static isValid(control: FormControl): any {
let initialValue = (' ' + control.value).slice(1);
if(initialValue === control.value){
return {
"changed": true
};
}
return null;
}
}
The intention was to clone the FormControl.value
, but I realize that this class is instantiated every time a value is changed in the FormControl.
One very hacky approach, that writes unreusable code is to fetch user.email
from the data provider
that stores it, and use it to compare it with the FormControl.value
. As you may guess, the validator will ONLY work for the User
object's parameters, and nothing else.
I want to write a universal Validator
that checks if the form's current value is different from the value that was initialized.
Edit:
This is a way to do it with pristine
, but it's not perfect.
import { FormControl } from '@angular/forms';
export class ChangedValidator {
static isValid(control: FormControl): any {
if(control.pristine){
return {
"unchanged": true
};
}
return null;
}
}