@@ -153,6 +153,9 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
153153 } ;
154154
155155 var builders = {
156+ sfField : function ( args ) {
157+ args . fieldFrag . firstChild . setAttribute ( 'sf-field' , args . path ) ;
158+ } ,
156159 ngModel : function ( args ) {
157160 if ( ! args . form . key ) {
158161 return ;
@@ -289,9 +292,6 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
289292 tmpl . appendChild ( div . childNodes [ 0 ] ) ;
290293 }
291294
292-
293- tmpl . firstChild . setAttribute ( 'sf-field' , path + '[' + index + ']' ) ;
294-
295295 // Possible builder, often a noop
296296 var args = {
297297 fieldFrag : tmpl ,
@@ -742,7 +742,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
742742 attributes `builder` and `replace` are optional, and replace defaults to true.
743743 */
744744 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.)
746746
747747 angular . forEach ( fields , function ( field , type ) {
748748 field . builder = field . builder || [ ] ;
@@ -816,7 +816,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
816816 decorator : function ( name ) {
817817 return decorators [ name ] || decorators [ defaultDecorator ] ;
818818 } ,
819- defaultDecorator : defaultDecorator
819+ defaultDecorator : defaultDecorator ,
820820 } ;
821821 } ;
822822
@@ -2066,6 +2066,7 @@ angular.module('schemaForm').directive('sfMessage',
20662066 // We only show one error.
20672067 // TODO: Make that optional
20682068 var error = errors [ 0 ] ;
2069+
20692070 if ( error ) {
20702071 element . html ( sfErrorMessage . interpolate (
20712072 error ,
@@ -2079,7 +2080,15 @@ angular.module('schemaForm').directive('sfMessage',
20792080 }
20802081 }
20812082 } ;
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+
20832092
20842093 scope . $watchCollection ( 'ngModel.$error' , function ( ) {
20852094 if ( scope . ngModel ) {
@@ -2284,7 +2293,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
22842293 if ( ! form ) {
22852294 return viewValue ;
22862295 }
2287-
2296+
22882297 // Omit TV4 validation
22892298 if ( scope . options && scope . options . tv4Validation === false ) {
22902299 return viewValue ;
@@ -2348,12 +2357,23 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
23482357 if ( ngModel . $validators ) {
23492358 ngModel . $validators . schemaForm = function ( ) {
23502359 // 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+ } ;
23532362 }
23542363
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+ }
23572377
23582378 // We set the viewValue to trigger parsers,
23592379 // since modelValue might be empty and validating just that
@@ -2377,8 +2397,10 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
23772397 // hence required works.
23782398 ngModel . $setViewValue ( ngModel . $viewValue ) ;
23792399 }
2400+ }
23802401
2381- } ) ;
2402+ // Listen to an event so we can validate the input on request
2403+ scope . $on ( 'schemaFormValidate' , scope . validateField ) ;
23822404
23832405 scope . schemaError = function ( ) {
23842406 return error ;
0 commit comments