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
+
1
27
function FpJsFormElement ( ) {
2
28
this . id = '' ;
3
29
this . name = '' ;
@@ -26,22 +52,43 @@ function FpJsFormElement() {
26
52
27
53
var self = this ;
28
54
var sourceId = 'form-error-' + String ( this . id ) . replace ( / _ / g, '-' ) ;
29
- self . errors [ sourceId ] = FpJsFormValidator . validateElement ( self ) ;
30
-
31
- var errorPath = FpJsFormValidator . getErrorPathElement ( self ) ;
32
- var domNode = errorPath . domNode ;
33
- if ( ! domNode ) {
34
- for ( var childName in errorPath . children ) {
35
- var childDomNode = errorPath . children [ childName ] . domNode ;
36
- if ( childDomNode ) {
37
- domNode = childDomNode ;
38
- break ;
55
+ this . clearErrorsRecursively ( sourceId ) ;
56
+
57
+ var validationErrors = FpJsFormValidator . validateElement ( self ) ;
58
+
59
+ var invalidTargets = { } ;
60
+ var validationError , errorTarget ;
61
+ for ( var v = 0 , vel = validationErrors . length ; v < vel ; ++ v ) {
62
+ validationError = validationErrors [ v ] ;
63
+ errorTarget = validationError . getTarget ( self ) ;
64
+
65
+ invalidTargets [ errorTarget . id ] = errorTarget ;
66
+
67
+ if ( ! errorTarget . errors [ sourceId ] ) {
68
+ errorTarget . errors [ sourceId ] = [ ] ;
69
+ }
70
+
71
+ errorTarget . errors [ sourceId ] . push ( validationError . message ) ;
72
+ }
73
+
74
+ for ( var id in invalidTargets ) {
75
+ self = invalidTargets [ id ] ;
76
+
77
+ var errorPath = FpJsFormValidator . getErrorPathElement ( self ) ;
78
+ var domNode = errorPath . domNode ;
79
+ if ( ! domNode ) {
80
+ for ( var childName in errorPath . children ) {
81
+ var childDomNode = errorPath . children [ childName ] . domNode ;
82
+ if ( childDomNode ) {
83
+ domNode = childDomNode ;
84
+ break ;
85
+ }
39
86
}
40
87
}
88
+ errorPath . showErrors . apply ( domNode , [ self . errors [ sourceId ] , sourceId ] ) ;
41
89
}
42
- errorPath . showErrors . apply ( domNode , [ self . errors [ sourceId ] , sourceId ] ) ;
43
90
44
- return self . errors [ sourceId ] . length == 0 ;
91
+ return validationErrors . length = == 0 ;
45
92
} ;
46
93
47
94
this . validateRecursively = function ( ) {
@@ -67,6 +114,24 @@ function FpJsFormElement() {
67
114
return true ;
68
115
} ;
69
116
117
+ this . clearErrors = function ( sourceId ) {
118
+ if ( ! sourceId ) {
119
+ for ( sourceId in this . errors ) {
120
+ this . clearErrors ( sourceId ) ;
121
+ }
122
+ } else {
123
+ this . errors [ sourceId ] = [ ] ;
124
+ this . showErrors . apply ( this . domNode , [ this . errors [ sourceId ] , sourceId ] ) ;
125
+ }
126
+ } ;
127
+
128
+ this . clearErrorsRecursively = function ( sourceId ) {
129
+ this . clearErrors ( sourceId ) ;
130
+ for ( var childName in this . children ) {
131
+ this . children [ childName ] . clearErrorsRecursively ( sourceId ) ;
132
+ }
133
+ } ;
134
+
70
135
this . showErrors = function ( errors , sourceId ) {
71
136
if ( ! ( this instanceof HTMLElement ) ) {
72
137
return ;
@@ -531,12 +596,19 @@ var FpJsFormValidator = new function () {
531
596
this . validateConstraints = function ( value , constraints , groups , owner ) {
532
597
var errors = [ ] ;
533
598
var i = constraints . length ;
599
+
534
600
while ( i -- ) {
535
601
if ( this . checkValidationGroups ( groups , constraints [ i ] ) ) {
536
602
errors = errors . concat ( constraints [ i ] . validate ( value , owner ) ) ;
537
603
}
538
604
}
539
605
606
+ for ( var e = 0 , el = errors . length ; e < el ; ++ e ) {
607
+ if ( typeof errors [ e ] === 'string' ) {
608
+ errors [ e ] = new FpJsFormError ( errors [ e ] ) ;
609
+ }
610
+ }
611
+
540
612
return errors ;
541
613
} ;
542
614
0 commit comments