-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8384659
Showing
2 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "qlack2-bower-angular-jsr-validation", | ||
"version": "1.0.0", | ||
"authors": [ | ||
"European Dynamics SA" | ||
], | ||
"description": "AngularJS module allowing to render JSR303-generated validation errors in forms.", | ||
"main": "src/QFormJSRValidationModule.js", | ||
"moduleType": [], | ||
"license": "EUPL", | ||
"homepage": "http://www.qlack.com", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* (c) EUROPEAN DYNAMICS SA <[email protected]> | ||
* | ||
* Licensed under the EUPL, Version 1.1 only (the "License"). | ||
* You may not use this work except in compliance with the Licence. | ||
* You may obtain a copy of the Licence at: | ||
* https://joinup.ec.europa.eu/software/page/eupl/licence-eupl | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Licence is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Licence for the specific language governing permissions and | ||
* limitations under the Licence. | ||
*/ | ||
angular.module('QFormJSRValidation', []) | ||
|
||
.provider('QFormJSRValidation', function() { | ||
this.$get = function() { | ||
return new QFormJSRValidationService(); | ||
}; | ||
}) | ||
|
||
.directive("fieldError", ["$state", "$injector", function($state, $injector) { | ||
return { | ||
restrict: "E", | ||
link: function(scope, element, attrs) { | ||
// Get a reference to the translation service, or create a mock | ||
// version of it if it's not available. | ||
var translate; | ||
if ($injector.has("$translate")) { | ||
translate = $injector.get("$translate"); | ||
} else { | ||
translate = { | ||
instant: function(key) { | ||
return (key); | ||
} | ||
} | ||
} | ||
|
||
// Find the form on which this element is embedded into. | ||
var form = $(element).closest("form"); | ||
|
||
// Find the name of the form on which this element is embedded into. | ||
var formName = form.attr("name"); | ||
|
||
// When the target form element becomes invalid, show validation errors. | ||
var validWatch = scope.$watch(formName + "[\'" + attrs.formElementName + "\'].$valid", function(newVal, oldVal) { | ||
if (newVal != undefined) { | ||
// Iterate through all errors. | ||
var errorHtml = "<ul>"; | ||
$.each(scope[formName][attrs.formElementName].$error, function (key, val) { | ||
errorHtml += "<li>" + translate.instant(key) + "</li>"; | ||
}); | ||
errorHtml += "</ul>"; | ||
element.html(errorHtml); | ||
$(element).show(); | ||
} | ||
}); | ||
|
||
// When the user changes the value of the target element, clear validation errors. | ||
var pristineWatch = scope.$watch(formName + "[\'" + attrs.formElementName + "\'].$pristine", function(newVal, oldVal) { | ||
if (newVal == false) { | ||
$(element).hide(); | ||
} | ||
}); | ||
|
||
scope.$on("$destroy", function() { | ||
validWatch(); | ||
pristineWatch(); | ||
}); | ||
} | ||
}; | ||
}]) | ||
|
||
.directive("formError", ["$state", function($state) { | ||
return { | ||
restrict: "E", | ||
link: function(scope, element, attrs) { | ||
// Find the form on which this element is embedded into. | ||
var form = $(element).closest("form"); | ||
|
||
// Find the name of the form on which this element is embedded into. | ||
var formName = form.attr("name"); | ||
|
||
// Create a watch for the generic form errors for this field. | ||
var errorWatch = scope.$watch(formName + ".$error", function(newVal, oldVal) { | ||
if (newVal != undefined) { | ||
if (newVal[attrs.dtoElementName] != undefined ) { | ||
$(element).show(); | ||
} | ||
} | ||
}, true); | ||
|
||
// Create watchers for the validity and pristine of each tracked | ||
// fields. When any of the tracked fields is changed, the generic | ||
// form error is removed. | ||
var trackFields = attrs.trackFields.split(","); | ||
var pristineWatch = []; | ||
$.each(trackFields, function(i, val) { | ||
pristineWatch.push(scope.$watch(formName + "[\'" + val + "\'].$pristine", function(newVal, oldVal) { | ||
if (newVal == false) { | ||
$(element).hide(); | ||
} | ||
})); | ||
}); | ||
|
||
scope.$on("$destroy", function() { | ||
errorWatch(); | ||
pristineWatch.forEach(function(callback) { | ||
callback(); | ||
}); | ||
}); | ||
} | ||
}; | ||
}]) | ||
|
||
.directive("showsValidationErrors", ["$state", function($state) { | ||
return { | ||
restrict: 'A', | ||
require: 'form', | ||
link: function (scope, element) { | ||
element.on('submit', function () { | ||
var form = scope[element.context.name]; | ||
// Reset field errors. | ||
$.each(form, function(key) { | ||
if (typeof form[key] == "object") { | ||
if (typeof form[key].$setPristine == "function" && key != "$$parentForm") { | ||
form[key].$setPristine(true); | ||
form[key].$valid = true; | ||
$.each(form[key].$error, function (errorKey) { | ||
form[key].$setValidity(errorKey, true); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
// Reset generic form errors. | ||
form.$error = {}; | ||
}); | ||
} | ||
}; | ||
}]); | ||
|
||
function QFormJSRValidationService() { | ||
this.markErrors = function($scope, form, data) { | ||
// Add errors. | ||
$.each(data, function(i, val) { | ||
// Before setting the error for the field we should check whether | ||
// such field does exist. This is necessary for @NotNull validation | ||
// errors of nested objects, since the error key in that case is the | ||
// name of the nested object itself. In such cases, the error is | ||
// added as a generic form error which can be displayed with the | ||
// form-error directive. | ||
if (form[val.path] !== undefined && typeof form[val.path].$setValidity == "function") { | ||
form[val.path].$setValidity(val.message, false); | ||
} else { | ||
form.$setValidity(val.path, false, val.message); | ||
} | ||
}); | ||
}; | ||
} |