$parser.unshift??how does this work?

27,419

Solution 1

Below is a step-by-step explanation. Notice the documentation is really good: the pages on the forms and on the $parsers are the ones you're looking for.

link: function(scope, elm, attrs, ctrl) {
    /**
     * This function is added to the list of the $parsers.
     * It will be executed the DOM (the view value) change.
     * Array.unshift() put it in the beginning of the list, so
     * it will be executed before all the other
     */
    ctrl.$parsers.unshift(function(viewValue) {

        scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined); // Check the length of the string
        scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains letter. RegExp.test() simply returns a boolean if the string matches the regex.
        scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined; // Check if the string contains digit. Same remark.

        if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) { // If all is good, then…
            ctrl.$setValidity('pwd', true); // Tell the controlller that the value is valid
            return viewValue; // Return this value (it will be put into the model)
        } else { // … otherwise…
            ctrl.$setValidity('pwd', false); // Tell the controlller that the value is invalid
            return undefined; // When the value is invalid, we should return `undefined`, as asked by the documentation
        }

    });
}

Solution 2

@Pawan Kalyan - Blackhole explained it very nicely (up-voted his answer). But I think you should also read this useful blog:-
$parsers and $formatters in Custom Validation Directives in Angular JS -

http://java.dzone.com/articles/parsers-and-formatters-custom

Happy Coding :)

Share:
27,419

Related videos on Youtube

Pawan Kalyan
Author by

Pawan Kalyan

Updated on June 25, 2020

Comments

  • Pawan Kalyan
    Pawan Kalyan about 4 years
     link: function(scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function(viewValue) {
    
                scope.pwdValidLength = (viewValue && viewValue.length >= 8 ? 'valid' : undefined);
                scope.pwdHasLetter = (viewValue && /[A-z]/.test(viewValue)) ? 'valid' : undefined;
                scope.pwdHasNumber = (viewValue && /\d/.test(viewValue)) ? 'valid' : undefined;
    
                if(scope.pwdValidLength && scope.pwdHasLetter && scope.pwdHasNumber) {
                    ctrl.$setValidity('pwd', true);
                    return viewValue;
                } else {
                    ctrl.$setValidity('pwd', false);                    
                    return undefined;
                }
    
            });
        }
    

    http://jsfiddle.net/adamdbradley/Qdk5M/

    In the above mentioned fiddle how does the password validation taking place? What does the $parser.unshift do?? and what is the use of test(viewValue).....? I have referred AngularJs main site but couldn't understand any thing... Please guide me step by step of how it is validating...

    I am new to angularJS..

  • dudewad
    dudewad almost 9 years
    Just to be clear, $parsers is an array (meaning, standard javascript type, not specific to Angular). unshift is a method native to the Javascript Array prototype, thus the comment in this answer of "Array.unshift() put it in the beginning of the list..." That's an important distinction, I think, because beginners may be confused between what is angular, and what is native JS.
  • OSdave
    OSdave almost 3 years
    link is broken.