we register custom-validation-methods for custom-form-elements via an extra-directives:
<ng-form name="validatorTestForm">
<our-input-directive name="validatorTest"
ng-model="ourModel"/>
</our-input-directive>
<our-validator-directive name="validatorTest"
form="validatorTestForm"
ng-model="ourModel">
</our-validator-directive>
</ng-form>
It gets all the information via attributes to know which input in which form to validate; then we connect it like that by initiating the directive: (stripped down version)
registerValidator(ourModel.form, 'validatorTest');
function registerValidator(form, inputName) {
var validationModel = form[inputName];
validationModel.$validators.testValidator = function (modelValue) {
// validate to true when there are more then two characters entered
return modelValue.length > 2;
};
}
The our-input-directive is quite simple:
angular.directive('ourInputDirective', function() {
return {
restrict: 'E',
template: '<input type="text" ng-model="model">',
scope: {
model: '=?ngModel'
}
}
});
So we run it, Angular does its magic and adds tons css-classes to the form-element and the input-element and when we input sth. into the input, it is properly triggering the validation. The form-element gets a css-class 'ng-valid' when it's valid and 'ng-invalid' when it's not.
The input howewer, just has the class 'ng-valid' and never becomes invalid!
So why is that and how can I change it, to reflect the model-changes on the inputs css-classes?
We want to utilize the 'ng-invalid' class to change the style of the input.