Angular - ngModelController missing $validators?
Asked Answered
E

1

8

I've been looking at Angular docs here: https://docs.angularjs.org/guide/forms#custom-validation

I'm trying to create my own input field validator using a custom directive. I've created a directive which seems identical to the one from the link above, only customized with my own validation function (6-digit password):

app.directive('password', function() {
   return {
       require: 'ngModel',
       link: function(scope, element, attrs, ctrl) {
           ctrl.$validators.password = function (modelValue, viewValue) {
               if (/^[0-9]{6}$/.test(viewValue)) {
                   return true;
               }

               return false;
           };
       }
   };
});

And when I run it, I get this error:

Error: ctrl.$validators is undefined

What am I missing here?

Esquiline answered 18/10, 2014 at 11:23 Comment(4)
Do you use Angular 1.3?Afroasiatic
1.2.26, that's currently the latest stable version.Esquiline
Then $validators doesn't exist yet.Deepdyed
Oh, ok. This explains it. Thanks.Esquiline
A
14

$validators only exist since version 1.3. Contrary to your comment the latest stable version is 1.3.0.

Afroasiatic answered 18/10, 2014 at 11:42 Comment(4)
How come Google doesn't host it yet then? o.O developers.google.com/speed/libraries/devguide#angularjsEsquiline
Google does host it.Deepdyed
@Johannes Today, after eight months of work, over two thousand commits, nineteen beta and six release candidates, we finally released AngularJS 1.3.0 superluminal-nudge- Oct. 14thAfroasiatic
Posted on angular-ui.github.io/bootstrap --> AngularJS (requires AngularJS 1.3.x, tested with 1.4.7). 0.12.0 is the last version of this library that supports AngularJS 1.2.x.Dunston

© 2022 - 2024 — McMap. All rights reserved.