Currently working with Angular 1.5
I have a good grasp on how angular validators are added to a field when the directive is triggered, and then all the validators fire when the control's value changes. For an amount field, I have implemented three different validations (correct characters, maximum length, and cannot be zero). In this case, I don't need to check for non-zero values if the field doesn't meet the valid amount criteria. Instead of re-checking all the valid amount conditions, I am interested in confirming if control.$validator.amountFormat.
is met first.
Is there any way to ensure that the custom format validator I created runs before the greater than zero validator? There are various other scenarios like this.
This is my current implementation:
ctrl.$validators.amountFormat = (modelValue: string, viewValue: string) => {
if (ctrl.$isEmpty(viewValue)) {
return true;
}
return isAmount(viewValue);
}
ctrl.$validators.amountGreaterThanZero = (modelValue: string, viewValue: string) => {
if (!isAmount(viewValue)) {
return true;
}
return parseFloat(viewValue) > 0;
}
This is my desired setup:
ctrl.$validators.amountGreaterThanZero = (modelValue: string, viewValue: string) => {
if (ctrl.$error.amountFormat) {
return true;
}
return parseFloat(viewValue) > 0;
}