I'm trying to build a module that has directive for custom validations and showing them using ng-messages
The validations are done via regex.
The error I am seeing is :
Error: [$compile:ctreq] Controller 'ngMessages', required by directive 'ngMessage', can't be found!
My code looks like this:
Validation directive:
module LoginModule {
'use strict';
/***** REGEX *****/
export class regExp {
public ID_OR_PASSPORT = /^[0-9]{9}$/;
public USERNAME_SINGLE_WORD = /^[A-Za-z0-9à-ú-_\.]{6,8}$/;
public PASSWORD = /^[A-Za-z0-9à-ú-_\.]{8}$/;
public EMAIL = /^([\?!\/]*)+\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
public ALPHANUMERIC = /^[a-zA-Z0-9]+$/;
public NUM = /^[0-9]{1,}[.]{0,1}[0-9]{0,}$/;
public PHONE_BODY = /^[0-9]{7}$/;
}
angular.module("LoginModule").value('REG_EXP', LoginModule.regExp);
}
module LoginModule {
export class uniIdValidator implements ng.IDirective {
constructor(public REG_EXP) { }
public restrict: string = 'A';
public require: string[] = ['ngModel'];
public templateUrl: string = 'errorMessages.html'; //this should be external file, will be used later
public replace: boolean = false;
public link: Function = (scope: ng.IScope,
element: ng.IAugmentedJQuery,
attrs: ng.IAttributes,
ctrls: any) => {
ctrls[0].$validators.userId = function (modelValue) {
//return REG_EXP.ID_OR_PASSPORT.test(modelValue);
console.log(modelValue)
return modelValue;
};
}
}
angular.module('LoginModule')
.directive('uniIdValidator', ['REG_EXP',
(REG_EXP) => { return new LoginModule.uniIdValidator(REG_EXP) }]);
}
In my html:
<Input ... uni-id-validator />
<div class="login-form__error-box" ng-messages="loginForm.loginFormId.$error">
<span class="login-form__error-msg" ng-message="userId">error in ID</span>
<span ng-message="required">This is required</span>
</div>
My app module:
((): void=> {
var appLogin = angular.module("LoginModule", ['ngRoute', 'ngMessages']);
appLogin.config(LoginModule.Routes.configureRoutes);
})()
My controller is too long to post here, but it has no inject of ngMessages (I tried using static inject as well --> didn't work)
Clearly I am doing something wrong, but I don't know what. (this is a continue to a problem I had before here)