Friday, October 24, 2014

reset valid for all form controls

I was trying to set $valid for all form controls, however I googled around and I do not see any solution to this on the internet. And I came out this myself as below:
    $scope.clearValidation = function () {
        angular.forEach($scope.managerUserForm, function (value, key) {
            var type = $scope.managerUserForm[key];
            if (type.$error) {
                angular.forEach(type.$error, function (value, key) {
                    type.$setValidity(key, 'true');                    
                });
            }
        });
    };
Or use this in modern browsers with ECMAScript 5 foreach:
Object.keys($scope.managerUserForm).forEach(function (key) {
            var type = $scope.managerUserForm[key];
            if (type.$error) {
                Object.keys(type.$error).forEach(function (key) {
                    type.$setValidity(key, 'true');                    
                });
            }
});
Or use underscore:
_.each($scope.managerUserForm, function(value, key){
    var type = value;
    if (type.$error) {
     _.each(type.$error, function(value, key){
        type.$setValidity(key, 'true'); 
     });
   }
});

No comments:

Post a Comment