How to revalidate a form with multiple dependent fields?

半世苍凉 提交于 2019-12-04 20:30:05

This is the solution I ended up with. Looks a bit hacked to me, but it works.

(function () {
    'use strict';

    angular
        .module('account')
        .directive('srbUniquePort', [srbUniquePort]);

    function srbUniquePort() {
        return {
            restrict: 'A',
            require: 'ngModel',
            scope: true,
            link: function (scope, element, attrs, ngModel) {

                function hasAValue(field) {
                    return !!field;
                }

                ngModel.$validators.srbUniquePort = function (val) {

                    var fieldName = attrs.name;

                    var configuration = scope.$eval(attrs.srbUniquePort);
                    var portFieldsToCheck = [
                        "dblibWebSrvcPort",
                        "dblibWebSrvcPortSLL",
                        "myRestServicePort",
                        "myRestServicePortSSL",
                        "alfrescoPortHttp",
                        "alfrescoPortHttps",
                        "alfrescoPortTomcatShutdown",
                        "alfrescoPortAJP",
                        "alfrescoPortMySql",
                        "alfrescoPortJOD",
                        "alfrescoPortVti"
                    ];
                    configuration[fieldName] = val;

                    if (scope.$parent.configuration == undefined) {
                        scope.$parent.configuration = JSON.parse(JSON.stringify(configuration));
                    }
                    scope.$parent.configuration[fieldName] = val;

                    // compare each port field with each other and in case if equality, 
                    // remember it by putting a "false" into the validityMap helper variable
                    var validityMap = [];
                    for (var i = 0; i < portFieldsToCheck.length; i++) {
                        for (var j = 0; j < portFieldsToCheck.length; j++) {
                            if (portFieldsToCheck[i] != portFieldsToCheck[j]) {

                                var iFieldHasAValue = hasAValue(scope.$parent.configuration[portFieldsToCheck[i]]);
                                var jFieldHasAValue = hasAValue(scope.$parent.configuration[portFieldsToCheck[j]]);
                                var valHasAValue = hasAValue(val);

                                if (iFieldHasAValue && jFieldHasAValue
                                    && scope.$parent.configuration[portFieldsToCheck[i]] == scope.$parent.configuration[portFieldsToCheck[j]]
                                    ) {
                                    validityMap[portFieldsToCheck[i]] = false;
                                    validityMap[portFieldsToCheck[j]] = false;
                                }
                            }
                        }
                    }

                    // in the end, loop through all port fields and set
                    // the validity here manually
                    for (var i = 0; i < portFieldsToCheck.length; i++) {
                        var valid = validityMap[portFieldsToCheck[i]];
                        if (valid == undefined) valid = true;
                        ngModel.$$parentForm[portFieldsToCheck[i]].$setValidity("srbUniquePort", valid);
                    }

                    // ending with the standard validation for the current field
                    for (var i = 0; i < portFieldsToCheck.length; i++) {
                        if (fieldName != portFieldsToCheck[i] && configuration[portFieldsToCheck[i]] == val) {
                            return false;
                        }
                    }
                    return true;
                }

            }
        }
    }
})();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!