@@ -317,7 +317,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
317317 // Do we have a condition? Then we slap on an ng-if on all children,
318318 // but be nice to existing ng-if.
319319 if ( form . condition ) {
320- angular . forEach ( element . children ( ) , function ( child ) {
320+ angular . forEach ( element . children ( ) , function ( child ) {
321321 var ngIf = child . getAttribute ( 'ng-if' ) ;
322322 child . setAttribute (
323323 'ng-if' ,
@@ -332,6 +332,45 @@ angular.module('schemaForm').provider('schemaFormDecorators',
332332 $compile ( element . contents ( ) ) ( scope ) ;
333333 } ) ;
334334
335+ // Where there is a key there is probably a ngModel
336+ if ( form . key ) {
337+ // It looks better with dot notation.
338+ scope . $on (
339+ 'schemaForm.error.' + form . key . join ( '.' ) ,
340+ function ( event , error , validationMessage , validity ) {
341+ if ( validationMessage === true || validationMessage === false ) {
342+ validity = validationMessage ;
343+ validationMessage = undefined ;
344+ }
345+
346+ if ( scope . ngModel && error ) {
347+ if ( scope . ngModel . $setDirty ( ) ) {
348+ scope . ngModel . $setDirty ( ) ;
349+ } else {
350+ // FIXME: Check that this actually works on 1.2
351+ scope . ngModel . $dirty = true ;
352+ scope . ngModel . $pristine = false ;
353+ }
354+
355+ // Set the new validation message if one is supplied
356+ // Does not work when validationMessage is just a string.
357+ if ( validationMessage ) {
358+ if ( ! form . validationMessage ) {
359+ form . validationMessage = { } ;
360+ }
361+ console . log ( 'settings validationMessage' , validationMessage )
362+ form . validationMessage [ error ] = validationMessage ;
363+ }
364+
365+ scope . ngModel . $setValidity ( error , validity === true ) ;
366+
367+ // Setting or removing a validity can change the field to believe its valid
368+ // but its not. So lets trigger its validation as well.
369+ scope . $broadcast ( 'schemaFormValidate' ) ;
370+ }
371+ } )
372+ }
373+
335374 once ( ) ;
336375 }
337376 } ) ;
@@ -1710,8 +1749,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
17101749 if ( ngModel . $setDirty ( ) ) {
17111750 ngModel . $setDirty ( ) ;
17121751 }
1713- validate ( ) ;
1714- //}
1752+ validate ( ngModel . $viewValue ) ;
17151753 } ) ;
17161754
17171755 scope . schemaError = function ( ) {
0 commit comments