ui-validate
ui-validate copied to clipboard
Validator function setting multiple types of errors
I need to have one function that validates a field that can have multiple error types. Splitting this validator to multiple separate functions is not practical due to the amount of code required to parse this input. I have created a my own variation of the uiValidate
directive with instead of:
ctrl.$validators[key] = validateFn;
having this:
ctrl.$validators[key] = function(value) {
if(ctrl._validatedKey) {
ctrl.$setValidity(ctrl._validatedKey, true);
delete ctrl._validatedKey;
}
var retVal = validateFn(value);
if(angular.isString(retVal)) {
ctrl.$setValidity(retVal, false);
ctrl._validatedKey = retVal;
return false;
}
return retVal;
};
So basically in a validator function instead of returning return false;
I can do return "errorFoo"
or return "errorBar"
depending on the error.
Is such a feature welcome in this library, should I bother with a pull request or keep my changes local?
+1
@Rush, it seems your suggestion is interesting for other people too. If you are still willing to submit a PR, I'd merge it.
+1
+1