1

I'm trying to build a module that has directive for custom validations.

The validations are done via regex.

The error I am seeing is:

Error: [$injector:unpr] Unknown provider: REG_EXPProvider <- REG_EXP <- uniIdValidatorDirective

My code looks like this:

config.ts:

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', regExp);
}

Validation directive:

module LoginModule {
    uniIdValidator.$inject = ['REG_EXP'];
    angular.module('LoginModule').directive('uniIdValidator', uniIdValidator);
    export function uniIdValidator(REG_EXP): ng.IDirective {
        return {
            restrict: 'A',
            require: ['ngModel'],
            templateUrl: 'errorMessages.html',
            replace: true,
            link: (scope: ng.IScope, element: ng.IAugmentedJQuery, 
                   attrs: ng.IAttributes, ctrls:any) => {
                ctrls.$validators.userId = function (modelValue) {
                    return REG_EXP.ID_OR_PASSPORT.test(modelValue);
                };
            }
        }
    };
}

In my html:

<Input ... uni-id-validator />

added my app.ts:

((): void=> {
    var appLogin = angular.module("LoginModule", ['ngRoute', 'ngMessages']);
    appLogin.config(LoginModule.Routes.configureRoutes);
})() 
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61

1 Answers1

1

There is a working plunker (showing that the below approach is able to create working directive with simplified REG_EXP implementation)

The way I do create directives with Typescript is similar to this:

module LoginModule {
    //uniIdValidator.$inject = ['REG_EXP'];
    //angular.module('LoginModule').directive('uniIdValidator', uniIdValidator);
    export class uniIdValidator implements ng.IDirective 
    {
        constructor(public REG_EXP) {}
        public restrict: string = 'A';
        public require: string = 'ngModel';
        public templateUrl: string = 'errorMessages.html';
        public replace: boolean = true;
        public link: Function = (scope: ng.IScope, 
            element: ng.IAugmentedJQuery,
            attrs: ng.IAttributes, 
            ctrls:any) => 
            {
                ctrls.$validators.userId = function (modelValue) 
                {
                    //return REG_EXP.ID_OR_PASSPORT.test(modelValue);
                    return "TO DO";
                };
            }          
    }

    angular.module('LoginModule')
      .directive('uniIdValidator', ['REG_EXP', 
          (REG_EXP) =>  {return new LoginModule.uniIdValidator(REG_EXP) });
}

Check it here. Here is a link to place which was used to transpile the TS to JS.

Some other link to Q & A (or here) with working examples...

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thanks for this, can you alter your example to include the input field? I didn't understand how to use it :/ – Ziv Weissman Sep 03 '15 at 13:59
  • I had to comment out your `return REG_EXP.ID_OR_PASSPORT.test(modelValue)` because I am not sure about the overall concept of that stuff. Other words, not sure what that directive should do. So there is another plunker, with a version which does log to console what you type in: http://plnkr.co/edit/HIOCzsh0tPSGep9MBQY9?p=preview hope it helps a bit – Radim Köhler Sep 03 '15 at 14:12
  • The directive should display error message if the regex of that input will fail. (using ng-message). Trying out your new version now. – Ziv Weissman Sep 03 '15 at 14:19
  • still not working :/ same error... are you missing "]" in the end of your before your last line? – Ziv Weissman Sep 03 '15 at 14:30
  • My plunker is working. I used just `require = 'ngModel'` because it is enough, just one is required. if you use `require = ['ngModel'];` then `ctrls` argument passed into link function is array. That means that if you want to use validators of it, you need to call it like this `ctrls[0].$validators.`... hope it helps a bit. I am trying to help with Typescript and angular. What you are doing with validators is a bit out of scope of this Q & A... I'd say... – Radim Köhler Sep 03 '15 at 15:11
  • I copy & paste from the pluncer as is, and typescript is showing me error missing "," at the last line, I've added "]" instead, and it compiles, yet still I have the same error :/ frustrated. – Ziv Weissman Sep 03 '15 at 15:20
  • Try this plunker with my latest version http://plnkr.co/edit/R67RBPK81rFBVcYGRQCp?p=preview ... typescript is 1 : 1 to javascript transpiled here http://www.typescriptlang.org/Playground – Radim Köhler Sep 03 '15 at 15:30
  • 1
    Thanks, I have made a progress, you are missing a ']', the plucker just renders to a valid js, like mine. (if you can edit it, its better though), Now I have error that missing 'ngMessage', trying to figure out why. Tnx! – Ziv Weissman Sep 03 '15 at 15:41
  • now can't get ngMessage to work... if you have the time to help out I will greatly appreciate it :) http://stackoverflow.com/questions/32421546/custom-validation-directive-using-ng-messages-and-typescript – Ziv Weissman Sep 06 '15 at 12:21
  • 1
    *I tried to replicate your issue, but I cannot get your error message ;( If you could create some plunker... with this issue, I can assist... Or show more of your code and full exception... whatever could get me more info* – Radim Köhler Sep 06 '15 at 12:51
  • While trying to reproduce the error on plunker, I realized what the problem was: ng-message in the html template. (I tried to remove it before of course but it saved a cache file of my template before the change...) – Ziv Weissman Sep 06 '15 at 15:22
  • Great, if that means you have solution! Great – Radim Köhler Sep 06 '15 at 15:29