Skip to content

Commit 687c277

Browse files
committed
Support for path mapping of error messages
1 parent 32ba8cc commit 687c277

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

Resources/public/js/FpJsFormValidator.js

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
function FpJsFormError(message) {
2+
this.message = message;
3+
this.atPath = null;
4+
5+
this.getTarget = function(rootElement) {
6+
if (!this.atPath) {
7+
return rootElement;
8+
}
9+
10+
var path = this.atPath.split('.');
11+
var targetElement = rootElement;
12+
var pathSegment;
13+
14+
while (pathSegment = path.shift()) {
15+
if (!targetElement.children[pathSegment]) {
16+
return targetElement;
17+
}
18+
19+
targetElement = targetElement.children[pathSegment];
20+
}
21+
22+
// fallback to rootElement in case the targetElement is not found
23+
return targetElement || rootElement;
24+
}
25+
}
26+
127
function FpJsFormElement() {
228
this.id = '';
329
this.name = '';
@@ -26,28 +52,47 @@ function FpJsFormElement() {
2652

2753
var self = this;
2854
var sourceId = 'form-error-' + String(this.id).replace(/_/g, '-');
29-
self.errors[sourceId] = [];
55+
this.clearErrorsRecursively(sourceId);
3056

3157
if (this.domNode && this.domNode.disabled) {
3258
return true;
3359
}
3460

35-
self.errors[sourceId] = FpJsFormValidator.validateElement(self);
61+
var validationErrors = FpJsFormValidator.validateElement(self);
3662

37-
var errorPath = FpJsFormValidator.getErrorPathElement(self);
38-
var domNode = errorPath.domNode;
39-
if (!domNode) {
40-
for (var childName in errorPath.children) {
41-
var childDomNode = errorPath.children[childName].domNode;
42-
if (childDomNode) {
43-
domNode = childDomNode;
44-
break;
63+
var invalidTargets = {};
64+
var validationError, errorTarget;
65+
for (var v = 0, vel = validationErrors.length; v < vel; ++v) {
66+
validationError = validationErrors[v];
67+
errorTarget = validationError.getTarget(self);
68+
69+
invalidTargets[errorTarget.id] = errorTarget;
70+
71+
if (!errorTarget.errors[sourceId]) {
72+
errorTarget.errors[sourceId] = [];
73+
}
74+
75+
errorTarget.errors[sourceId].push(validationError.message);
76+
}
77+
78+
for (var id in invalidTargets) {
79+
self = invalidTargets[id];
80+
81+
var errorPath = FpJsFormValidator.getErrorPathElement(self);
82+
var domNode = errorPath.domNode;
83+
if (!domNode) {
84+
for (var childName in errorPath.children) {
85+
var childDomNode = errorPath.children[childName].domNode;
86+
if (childDomNode) {
87+
domNode = childDomNode;
88+
break;
89+
}
4590
}
4691
}
92+
errorPath.showErrors.apply(domNode, [self.errors[sourceId], sourceId]);
4793
}
48-
errorPath.showErrors.apply(domNode, [self.errors[sourceId], sourceId]);
4994

50-
return self.errors[sourceId].length == 0;
95+
return validationErrors.length === 0;
5196
};
5297

5398
this.validateRecursively = function () {
@@ -73,6 +118,24 @@ function FpJsFormElement() {
73118
return true;
74119
};
75120

121+
this.clearErrors = function(sourceId) {
122+
if (!sourceId) {
123+
for (sourceId in this.errors) {
124+
this.clearErrors(sourceId);
125+
}
126+
} else {
127+
this.errors[sourceId] = [];
128+
this.showErrors.apply(this.domNode, [this.errors[sourceId], sourceId]);
129+
}
130+
};
131+
132+
this.clearErrorsRecursively = function (sourceId) {
133+
this.clearErrors(sourceId);
134+
for (var childName in this.children) {
135+
this.children[childName].clearErrorsRecursively(sourceId);
136+
}
137+
};
138+
76139
this.showErrors = function (errors, sourceId) {
77140
if (!(this instanceof HTMLElement)) {
78141
return;
@@ -541,12 +604,19 @@ var FpJsFormValidator = new function () {
541604
this.validateConstraints = function (value, constraints, groups, owner) {
542605
var errors = [];
543606
var i = constraints.length;
607+
544608
while (i--) {
545609
if (this.checkValidationGroups(groups, constraints[i])) {
546610
errors = errors.concat(constraints[i].validate(value, owner));
547611
}
548612
}
549613

614+
for (var e = 0, el = errors.length; e < el; ++e) {
615+
if (typeof errors[e] === 'string') {
616+
errors[e] = new FpJsFormError(errors[e]);
617+
}
618+
}
619+
550620
return errors;
551621
};
552622

0 commit comments

Comments
 (0)