Let's say we need a custom Angular validator that provides arguments to the error message. For instance if we were to create a validator that would evaluate the minimum length of an array and when it was invalid the message should be "The minimum length is {0}".
Now considering we have created a custom directive(as the one provided below) for this validation, how do we provide the "{0}" argument from the validation directive function to the validation message?
Here's a custom validation directive for the that:
.directive('arrayLength', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$validators.arrayLength = function (modelValue, viewValue) {
return modelValue >= parseInt(attrs.arrayLength);
};
}
};
});
And here where we would be declaring the message:
.run([
'defaultErrorMessageResolver',
function (defaultErrorMessageResolver) {
defaultErrorMessageResolver.getErrorMessages().then(function (errorMessages) {
errorMessages['arrayLength'] = 'Minimum length is {0}';
});
}
]);
So... any ideas?
I'm sure it's not a rocket science. It's just that I'm new to Angular.
Thanks in advance.