@@ -153,6 +153,9 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
153
153
} ;
154
154
155
155
var builders = {
156
+ sfField : function ( args ) {
157
+ args . fieldFrag . firstChild . setAttribute ( 'sf-field' , args . path ) ;
158
+ } ,
156
159
ngModel : function ( args ) {
157
160
if ( ! args . form . key ) {
158
161
return ;
@@ -289,9 +292,6 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
289
292
tmpl . appendChild ( div . childNodes [ 0 ] ) ;
290
293
}
291
294
292
-
293
- tmpl . firstChild . setAttribute ( 'sf-field' , path + '[' + index + ']' ) ;
294
-
295
295
// Possible builder, often a noop
296
296
var args = {
297
297
fieldFrag : tmpl ,
@@ -742,7 +742,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
742
742
attributes `builder` and `replace` are optional, and replace defaults to true.
743
743
*/
744
744
this . defineDecorator = function ( name , fields ) {
745
- decorators [ name ] = { '__name' : name } ; // TODO: this feels like a hack, come up with a better way.
745
+ decorators [ name ] = { '__name' : name } ; // TODO: this feels like a hack, come up with a better way. (ES6 Symbols would be a nice fit.)
746
746
747
747
angular . forEach ( fields , function ( field , type ) {
748
748
field . builder = field . builder || [ ] ;
@@ -816,7 +816,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
816
816
decorator : function ( name ) {
817
817
return decorators [ name ] || decorators [ defaultDecorator ] ;
818
818
} ,
819
- defaultDecorator : defaultDecorator
819
+ defaultDecorator : defaultDecorator ,
820
820
} ;
821
821
} ;
822
822
@@ -2066,6 +2066,7 @@ angular.module('schemaForm').directive('sfMessage',
2066
2066
// We only show one error.
2067
2067
// TODO: Make that optional
2068
2068
var error = errors [ 0 ] ;
2069
+
2069
2070
if ( error ) {
2070
2071
element . html ( sfErrorMessage . interpolate (
2071
2072
error ,
@@ -2079,7 +2080,15 @@ angular.module('schemaForm').directive('sfMessage',
2079
2080
}
2080
2081
}
2081
2082
} ;
2082
- update ( ) ;
2083
+
2084
+ // When link occurs we might not have form with the new builder.
2085
+ var once = scope . $watch ( 'form' , function ( form ) {
2086
+ if ( form ) {
2087
+ update ( ) ;
2088
+ once ( ) ;
2089
+ }
2090
+ } ) ;
2091
+
2083
2092
2084
2093
scope . $watchCollection ( 'ngModel.$error' , function ( ) {
2085
2094
if ( scope . ngModel ) {
@@ -2284,7 +2293,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2284
2293
if ( ! form ) {
2285
2294
return viewValue ;
2286
2295
}
2287
-
2296
+
2288
2297
// Omit TV4 validation
2289
2298
if ( scope . options && scope . options . tv4Validation === false ) {
2290
2299
return viewValue ;
@@ -2348,12 +2357,23 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2348
2357
if ( ngModel . $validators ) {
2349
2358
ngModel . $validators . schemaForm = function ( ) {
2350
2359
// Any error and we're out of here!
2351
- return ! Object . keys ( ngModel . $error ) . some ( function ( e ) { return e !== 'schemaForm' } ) ;
2352
- }
2360
+ return ! Object . keys ( ngModel . $error ) . some ( function ( e ) { return e !== 'schemaForm' ; } ) ;
2361
+ } ;
2353
2362
}
2354
2363
2355
- // Listen to an event so we can validate the input on request
2356
- scope . $on ( 'schemaFormValidate' , function ( ) {
2364
+ var schema = form . schema ;
2365
+
2366
+ // A bit ugly but useful.
2367
+ scope . validateField = function ( ) {
2368
+
2369
+ // Special case: arrays
2370
+ // TODO: Can this be generalized in a way that works consistently?
2371
+ // Just setting the viewValue isn't enough to trigger validation
2372
+ // since it's the same value. This will be better when we drop
2373
+ // 1.2 support.
2374
+ if ( schema && schema . type . indexOf ( 'array' ) !== - 1 ) {
2375
+ validate ( ngModel . $modelValue ) ;
2376
+ }
2357
2377
2358
2378
// We set the viewValue to trigger parsers,
2359
2379
// since modelValue might be empty and validating just that
@@ -2377,8 +2397,10 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2377
2397
// hence required works.
2378
2398
ngModel . $setViewValue ( ngModel . $viewValue ) ;
2379
2399
}
2400
+ }
2380
2401
2381
- } ) ;
2402
+ // Listen to an event so we can validate the input on request
2403
+ scope . $on ( 'schemaFormValidate' , scope . validateField ) ;
2382
2404
2383
2405
scope . schemaError = function ( ) {
2384
2406
return error ;
0 commit comments