I have found great many occurrences of the following pattern for html inputs, this being for phone numbers:
<input type="text" ng-model="CellPhoneNumber" required ng-pattern="/^[0-9]+$/" ng-minlength="10" />
I would like to create a custom directive that, wherever applied, will tell Angular to apply all three of these rules, e.g:
<input type="text" ng-model="CellPhoneNumber" bk-ng-validation="phoneNumber"/>
Then, code in my directive would find and invoke a function called phoneNumber
, in which I would like to see something like:
Listing 1:
function bkNgPhoneNumber(model) {
// This is purely SPECULATIVE pseudo-code, just to convey an idea.
model.errors.add(applyMinLength(10, model));
model.errors.add(applyMaxLength(15, model));
model.errors.add(applyPattern("/^[0-9]+$/", model));
}
I would prefer the above approach over 'rewriting code for these rules, e.g:
Listing 2:
function phoneNumber(model) {
if (model.length < 10 || model.length > 15) {
model.errors.add("Must be 10 to 15 chars!");
}
}
I don't want to do away with all attribute based directives, but preferably create a 'macro' directive that will invoke my Listing 1 code, which will intern invoke a set of more 'micro' validations.
You can try this approach:
If you're using more validations, you can create a service that is responsible for identifying and validating the elements, without any limitation. Default directives of angular remain.
Example:
Demo
You are going in the opposite way, because you are assuming that directives are that much laborious to maintain and want to keep one to give all the validation you need, depending on the element.
That's an interesting approach but you need to be warned about the modularity of this approach: give this much of labour to one directive only is counterintuitive over the best practices of doing a "pure Angular way" to do the things.
If you want to proceed with this idea, I suggest you to look over the
ngModelController
(AngularJS Docs) properties, that can be injected onlink()
function of one directive. More precisely, the$validators
.You can add how many
$validators
to a NgModel controller you want.During your validation, you can set/unset validity to the element returning a boolean:
I suggest you to read more about this implementation because certain operations can interrupt the flux of
$digest
cycles on angular over the manipulated element.Edit 1:
Like I've mentioned on the comments, here's a Plunkr with an working example.
One way to do this (i.e. apply existing validators without writing their code again) would be to add the validation directives' respective attributes and force a re-compile. This would require your directive to have a high-enough priority and also be
terminal: true
.Demo
You can create a new component which including control with all required validators. Your component would look like:
All required logic component should keep inside. To do it, create the
my-control
directive with template. Inside the template you can place an input with validation attributes:Then you need to bind ng-model value on your component to input:
Here is a demo when you can see this component in action and how it works.