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+
127function FpJsFormElement ( ) {
228 this . id = '' ;
329 this . name = '' ;
@@ -26,22 +52,43 @@ function FpJsFormElement() {
2652
2753 var self = this ;
2854 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+ }
3986 }
4087 }
88+ errorPath . showErrors . apply ( domNode , [ self . errors [ sourceId ] , sourceId ] ) ;
4189 }
42- errorPath . showErrors . apply ( domNode , [ self . errors [ sourceId ] , sourceId ] ) ;
4390
44- return self . errors [ sourceId ] . length == 0 ;
91+ return validationErrors . length = == 0 ;
4592 } ;
4693
4794 this . validateRecursively = function ( ) {
@@ -67,6 +114,24 @@ function FpJsFormElement() {
67114 return true ;
68115 } ;
69116
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+
70135 this . showErrors = function ( errors , sourceId ) {
71136 if ( ! ( this instanceof HTMLElement ) ) {
72137 return ;
@@ -531,12 +596,19 @@ var FpJsFormValidator = new function () {
531596 this . validateConstraints = function ( value , constraints , groups , owner ) {
532597 var errors = [ ] ;
533598 var i = constraints . length ;
599+
534600 while ( i -- ) {
535601 if ( this . checkValidationGroups ( groups , constraints [ i ] ) ) {
536602 errors = errors . concat ( constraints [ i ] . validate ( value , owner ) ) ;
537603 }
538604 }
539605
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+
540612 return errors ;
541613 } ;
542614
0 commit comments