Created
May 23, 2014 11:54
-
-
Save PawelGerr/f937c899cb0e84ef05d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
/** | |
* @param $scope | |
*/ | |
function TtValidateController($scope) { | |
var validationKey = 'ttValidate-validate'; | |
$scope.ngModel = null; | |
$scope.validationFunction = null; | |
$scope.message = null; | |
this.ngModelExpression = null; | |
this.ngRepeats = null; | |
this.applyValidationDefinition = function (validationDefinition, modelGetter) { | |
$scope.validationFunction = createValidationFunction(validationDefinition, modelGetter); | |
$scope.ngModel.$formatters.push($scope.validationFunction); | |
$scope.ngModel.$parsers.push($scope.validationFunction); | |
}; | |
function handleValidationResult(validationResult) { | |
$scope.message = validationResult.messages; | |
if (angular.isArray($scope.message)) | |
$scope.message = $scope.message.join(', '); | |
}; | |
function createValidationFunction(validationDefinition, modelGetter) { | |
return function (value) { | |
var validationResult; | |
/* | |
* Execution priority | |
* 1: isRequired | |
* 2: validate | |
*/ | |
if (angular.isDefined(validationDefinition.isRequired)) { | |
var isValid = !$scope.ngModel.$isEmpty(value); | |
validationResult = { | |
isValid: isValid, | |
messages: isValid ? null : validationDefinition.isRequired.messages | |
}; | |
} | |
if ((!validationResult || validationResult.isValid) && validationDefinition.validate) { | |
var result = validationDefinition.validate(value, modelGetter()); | |
validationResult = mergeValidationResults(validationResult, result); | |
} | |
if (validationResult) { | |
handleValidationResult(validationResult); | |
if (validationResult.isValid) { | |
$scope.ngModel.$setValidity(validationKey, true); | |
return value; | |
} else { | |
$scope.ngModel.$setValidity(validationKey, false); | |
return; | |
} | |
} | |
}; | |
} | |
} | |
function mergeValidationResults(result, otherResult) { | |
if (!result) | |
return otherResult; | |
if (!otherResult) | |
return result; | |
return { | |
isValid: result.isValid && otherResult.isValid, | |
messages: concatArrays(result.messages, otherResult.messages) | |
}; | |
} | |
function concatArrays(array, otherArray) { | |
if (!array) | |
return otherArray; | |
if (!otherArray) | |
return array; | |
var mergedArray = []; | |
for (var i = 0; i < array.length; i++) { | |
mergedArray.push(array[i]); | |
} | |
for (var i = 0; i < otherArray.length; i++) { | |
mergedArray.push(otherArray[i]); | |
} | |
return mergedArray; | |
} | |
function Directive($compile) { | |
return { | |
restrict: 'A', | |
require: ['ngModel', '^ttValidation', 'ttValidate'], | |
scope: true, | |
controller: TtValidateController, | |
compile: function (tElement, tAttrs, transclude) { | |
return { | |
pre: function (scope, element, attrs) { | |
}, | |
/** | |
* @param scope | |
* @param element | |
* @param attrs | |
* @param {Array} controllers | |
*/ | |
post: function (scope, element, attrs, controllers) { | |
var messageElement = angular.element('<div ng-show="message" class="alert alert-danger">{{message}}</div>'); | |
element.after(messageElement); | |
$compile(messageElement)(scope); | |
var ttValidation = controllers[1]; | |
var controller = controllers[2]; | |
scope.ngModel = controllers[0]; | |
controller.ngModelExpression = attrs.ngModel; | |
controller.ngRepeats = ttValidation.getNgRepeats(element, controller.ngModelExpression); | |
ttValidation.addValidationControl(controller); | |
scope.$on('$destroy', function () { | |
ttValidation.removeValidationControl(controller); | |
if (scope.validationFunction) { | |
scope.ngModel.$formatters.indexOf(scope.validationFunction); | |
scope.ngModel.$parsers.indexOf(scope.validationFunction); | |
} | |
}); | |
} | |
}; | |
} | |
}; | |
} | |
app.module.directive("ttValidate", Directive); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment