Skip to content

fix jshint error and improve some features... #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
187 changes: 102 additions & 85 deletions validationdirectives.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,114 @@
'use strict';

/**
* Anularjs Module for form validation directives
* http://blog.technovert.com/2014/03/angularjs-form-validation-library-directives/#remote
*/
angular.module('validation', [])
.directive('customValidator', [function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
var validateFunctionNames = attr["validateFunctions"].split(",");
var validatorNames = attr["customValidator"].split(",");
ngModelCtrl.$parsers.push(function (value) {
var hasErrors = false;
angular.forEach(validateFunctionNames, function (functionName, index) {
if (!scope[functionName]) {
console.log('There is no function with name ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
} else {
var result = scope[functionName](value);
if (result && result != false) {
ngModelCtrl.$setValidity(validatorNames[index], true);
.directive('customValidator', ['$log', function ($log) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
var validateFunctionNames = attr.validateFunctions.split(',');
var validatorNames = attr.customValidator.split(',');
ngModelCtrl.$parsers.push(function (value) {
var hasErrors = false;
angular.forEach(validateFunctionNames, function (functionName, index) {
if (!scope[functionName]) {
$log.log('There is no function with name ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
} else {
ngModelCtrl.$setValidity(validatorNames[index], false);
hasErrors = true;
}

}
});
return hasErrors ? undefined : value;
});
}
};
}])
.directive('customRemoteValidator', [function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
var validateFunctionNames = attr["remoteValidateFunctions"].split(",");
var validatorNames = attr["customRemoteValidator"].split(",");
ngModelCtrl.$parsers.push(function (value) {
angular.forEach(validateFunctionNames, function (functionName, index) {
if (!scope[functionName]) {
console.log('There is no function with ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
} else {
var result = scope[functionName](value);
if (result.then) {
result.then(function (data) { //For promise type result object
ngModelCtrl.$setValidity(validatorNames[index], data);
}, function (error) {
var result = scope[functionName](value);
if (result && result !== false) {
ngModelCtrl.$setValidity(validatorNames[index], true);
} else {
ngModelCtrl.$setValidity(validatorNames[index], false);
});
hasErrors = true;
}

}
}
});
return hasErrors ? undefined : value;
});
return value;
});
}
};
}])
.directive('validationMessages', function () {
return {
scope: {
modelController: '='
},
restrict: 'EA',
link: function (scope, elm, attrs) {
if (!scope.modelController) {
console.log('Requires a html attribute data-model-controller. This should point to the input field model controller.');
}
scope.$watch('modelController.$error', function (newValue) {
if (newValue) {
scope.errorMessages = [];
angular.forEach(newValue, function (value, key) {
if (value && attrs[key + 'Error']) {
scope.errorMessages.push(attrs[key + 'Error']);
};
}])
.directive('customRemoteValidator', ['$log', function ($log) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attr, ngModelCtrl) {
var validateFunctionNames = attr.remoteValidateFunctions.split(',');
var validatorNames = attr.customRemoteValidator.split(',');
ngModelCtrl.$parsers.push(function (value) {
angular.forEach(validateFunctionNames, function (functionName, index) {
if (!scope[functionName]) {
$log.log('There is no function with ' + functionName + ' available on the scope. Please make sure the function exists on current scope or its parent.');
} else {
var result = scope[functionName](value);
if (result.then) {
result.then(function (data) { //For promise type result object
ngModelCtrl.$setValidity(validatorNames[index], data);
}, function (/*error*/) {
ngModelCtrl.$setValidity(validatorNames[index], false);
});
}
}
});
return value;
});
}
};
}])
.directive('validationMessages', ['$log', function ($log) {
return {
scope: {
modelController: '='
},
restrict: 'EA',
link: function (scope, elm, attrs) {
if (!scope.modelController) {
$log.log('Requires a html attribute data-model-controller. This should point to the input field model controller.');
}
scope.$watch('modelController.$error', function (newValue) {
if (newValue) {
scope.errorMessages = [];
angular.forEach(newValue, function (value, key) {
if (value && attrs[key + 'Error']) {
scope.errorMessages.push(attrs[key + 'Error']);
}
});
}
}, true);
},
template: '<div><small class="error" ng-repeat="message in errorMessages" ng-show= "!modelController.$pristine && $first" class="warning">{{message}}</small></div>'
};
}])
.directive('updateOnBlur', [function () {
return {
restrict: 'A',
require: 'ngModel',
priority: '100',
link: function (scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') {
return;
}
}, true);
},
template: '<div><small class="error" ng-repeat="message in errorMessages" ng-show= "!modelController.$pristine && $first" class="warning">{{message}}</small></div>'
}
})
.directive('updateOnBlur', function () {
return {
restrict: 'A',
require: 'ngModel',
priority: '100',
link: function (scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;

elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur', function () {
scope.$apply(function () {
function updateNgModel() {
ngModelCtrl.$setViewValue(elm.val());
}

elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur submit', function () {
updateNgModel();
if (!scope.$root.$$phase) {
scope.$apply();
}
});

scope.$on('$destroy', function () {
elm.unbind('blur').unbind('submit');
});
});
}
};
});
}
};
}]);