Skip to content

Commit

Permalink
Support for deep references
Browse files Browse the repository at this point in the history
  • Loading branch information
NMichas committed May 3, 2017
1 parent 8384659 commit b4ae83c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qlack2-bower-angular-jsr-validation",
"version": "1.0.0",
"version": "1.0.1",
"authors": [
"European Dynamics SA"
],
Expand Down
16 changes: 12 additions & 4 deletions src/QFormJSRValidationModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ angular.module('QFormJSRValidation', [])

// When the target form element becomes invalid, show validation errors.
var validWatch = scope.$watch(formName + "[\'" + attrs.formElementName + "\'].$valid", function(newVal, oldVal) {
if (newVal != undefined) {
if (newVal != undefined && newVal == false) {
// Iterate through all errors.
var errorHtml = "<ul>";
$.each(scope[formName][attrs.formElementName].$error, function (key, val) {
$.each(deep_value(scope, formName)[attrs.formElementName].$error, function (key, val) {
errorHtml += "<li>" + translate.instant(key) + "</li>";
});
errorHtml += "</ul>";
Expand Down Expand Up @@ -92,7 +92,7 @@ angular.module('QFormJSRValidation', [])
}, true);

// Create watchers for the validity and pristine of each tracked
// fields. When any of the tracked fields is changed, the generic
// field. When any of the tracked fields is changed, the generic
// form error is removed.
var trackFields = attrs.trackFields.split(",");
var pristineWatch = [];
Expand Down Expand Up @@ -120,7 +120,7 @@ angular.module('QFormJSRValidation', [])
require: 'form',
link: function (scope, element) {
element.on('submit', function () {
var form = scope[element.context.name];
var form = deep_value(scope, element.context.name);
// Reset field errors.
$.each(form, function(key) {
if (typeof form[key] == "object") {
Expand All @@ -141,6 +141,14 @@ angular.module('QFormJSRValidation', [])
};
}]);

/** Find an object possible deeply nested */
var deep_value = function(obj, path){
for (var i=0, path=path.split('.'), len=path.length; i<len; i++){
obj = obj[path[i]];
};
return obj;
};

function QFormJSRValidationService() {
this.markErrors = function($scope, form, data) {
// Add errors.
Expand Down

0 comments on commit b4ae83c

Please sign in to comment.