I've been learning validation forms with angular. I'm using ng-pattern to define the correct format of the input. But after customizing the classes depending on the element's parameters ($valid, $invalid, $error) it requires a lot of code to define the css behaviour related to validation.
So I am exploring the option to build a directive element which could contain all the necessary behaviour.
Here's a plunker illustrating this: plunker
Here's the way I would implement the form so I only need to stack the directive elements with their attributes like this : (example here with 2 inputs, but could be more)
<form role="form" class="form-horizontal" name="signup_form">
<input-validation
ng-model="register.age"
namevalue="age"
formvalue="signup_form"
labeltext="What's your Age?"
patterntext="/^[0-9]{1,2}$/"
errortext="Age must between 1 and 99"
placeholdertext="Enter your Age"></input-validation>
<input-validation
ng-model="register.firstname"
namevalue="firstname"
formvalue="signup_form"
labeltext="What's your Name?"
patterntext="/^[a-zA-Z0-9]{4,20}$/"
errortext="Name must between 1 and 20 characters long"
placeholdertext="Enter your Name"></input-validation>
</form>
With a directive as such :
app.directive('inputValidation', function(){
return{
restrict: 'E',
templateUrl : 'inputValidation.html',
scope: {
inputtext: '=ngModel',
formvalue: '=',
namevalue: '=',
labeltext: '@',
errortext: '@',
placeholdertext: '@',
patterntext: '@',
autofocusvalue: '@'
}
};
});
But this doesn't work properly. I understand that the solution could be a combination of compile, require inside the directive, or using the link with the 4th parameter (ctrl) but I'm not sure how to implement this and could use some help.