覆盖AngularJS URL验证器

Bla*_*ise 6 validation angularjs

AngularJS接受这个有效的URL:

var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
Run Code Online (Sandbox Code Playgroud)

Django接受这个:

regex = re.compile(
    r'^(?:http|ftp)s?://' # http:// or https://
    r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
    r'localhost|' #localhost...
    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
    r'(?::\d+)?' # optional port
    r'(?:/?|[/?]\S+)$', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)

主要的实际区别是AngularJS接受http://some-host-without-tld,而Django只允许http://localhost作为没有TLD的有效URL.

由于覆盖Django URL Validator存在问题,我想覆盖AngularJS验证器.我试过这种方式,这有效:

app.overwriteUrlValidator = function(ngModel) {

    // Django RegExp, ported to JS.
    var URL_REGEXP = /^(?:http|ftp)s?:\/\/(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|localhost|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d+)?(?:\/?|[\/?]\S+)$/gi;

    // Same validator as AngularJS's, only with a different RegExp.
    function urlValidator(value) {
        if (ngModel.$isEmpty(value) || URL_REGEXP.test(value)) {
            ngModel.$setValidity('url', true);
            return value;
        } else {
            ngModel.$setValidity('url', false);
            return undefined;
        }
    }

    // This is the part I'm not happy about. I need to use a timeout
    // because my code is executed before Angular adds its URL validator.
    // If I add mine before Angular does, it will not work for ??? reason.
    window.setTimeout(function() {
        ngModel.$formatters.push(urlValidator);
        ngModel.$parsers.push(urlValidator);
    }, 100);
};


/**
 * Keep track of user's interaction with input fields
 */
app.inputDirective = function() {
    function link(scope, element, attrs, ngModel) {
        if (ngModel && attrs.type === 'url') {
            app.overwriteUrlValidator(ngModel);
        }
    }

    return {
        restrict: 'A',
        require : '?ngModel',
        link    : link
    };
};
app.directive('input', app.inputDirective);
app.directive('textarea', app.inputDirective);
Run Code Online (Sandbox Code Playgroud)

我宁愿不切换到另一个验证指令,因为我必须更新并检查很多代码.

有谁知道这样做的可靠方法?

Bla*_*ise 6

从Angular 1.3开始,现在相对容易,你可以完全覆盖现有的$validators:

myApp.directive('input', function() {
    function link(scope, element, attrs, ngModel) {
        function allowSchemelessUrls() {
            // Match Django's URL validator, which allows schemeless urls.
            var URL_REGEXP = /^((?:http|ftp)s?:\/\/)(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|localhost|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d+)?(?:\/?|[\/?]\S+)$/i;

            // Silently prefixes schemeless URLs with 'http://' when 
            // converting a view value to model value.    
            ngModel.$parsers.unshift(function(value) {
                if (!URL_REGEXP.test(value) && URL_REGEXP.test('http://' + value)) {
                    return 'http://' + value;
                } else {
                    return value;
                }
            });

            ngModel.$validators.url = function(value) {
                return ngModel.$isEmpty(value) || URL_REGEXP.test(value);
            };
        }

        if (ngModel && attrs.type === 'url') {
            allowSchemelessUrls();
        }
    }

    return {
        require: '?ngModel',
        link: link
    };
});
Run Code Online (Sandbox Code Playgroud)