@@ -271,7 +271,6 @@ var transclusion = function() {
271271 * Builds a form from a canonical form definition
272272 */
273273 build : function ( form , decorator , slots ) {
274- console . warn ( slots )
275274 return build ( form , decorator , function ( url ) {
276275 return $templateCache . get ( url ) || '' ;
277276 } , slots ) ;
@@ -748,9 +747,9 @@ angular.module('schemaForm').provider('schemaFormDecorators',
748747 this . addMapping = function ( name , type , url , builder , replace ) {
749748 if ( decorators [ name ] ) {
750749 decorators [ name ] [ type ] = {
751- temlpate : url ,
750+ template : url ,
752751 builder : builder ,
753- replace : replace
752+ replace : ! ! replace
754753 } ;
755754 }
756755 } ;
@@ -2029,13 +2028,20 @@ angular.module('schemaForm').directive('sfMessage',
20292028 element . html ( msg ) ;
20302029 } else {
20312030
2032- var errors = Object . keys (
2033- ( scope . ngModel && scope . ngModel . $error ) || { }
2034- ) ;
20352031
2036- // Since we use $parsers to hook up our validation we also end up with a "parse" error.
2037- // so we remove it.
2038- errors = errors . filter ( function ( e ) { return e !== 'parse' ; } ) ;
2032+ var errors = [ ] ;
2033+ angular . forEach ( ( ( scope . ngModel && scope . ngModel . $error ) || { } ) , function ( status , code ) {
2034+ if ( status ) {
2035+ // if true then there is an error
2036+ // Angular 1.3 removes properties, so we will always just have errors.
2037+ // Angular 1.2 sets them to false.
2038+ errors . push ( code ) ;
2039+ }
2040+ } ) ;
2041+
2042+ // In Angular 1.3 we use one $validator to stop the model value from getting updated.
2043+ // this means that we always end up with a 'schemaForm' error.
2044+ errors = errors . filter ( function ( e ) { return e !== 'schemaForm' ; } ) ;
20392045
20402046 // We only show one error.
20412047 // TODO: Make that optional
@@ -2265,6 +2271,8 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
22652271 }
22662272
22672273 var result = sfValidator . validate ( form , viewValue ) ;
2274+
2275+
22682276 // Since we might have different tv4 errors we must clear all
22692277 // errors that start with tv4-
22702278 Object . keys ( ngModel . $error )
@@ -2275,6 +2283,15 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
22752283 // it is invalid, return undefined (no model update)
22762284 ngModel . $setValidity ( 'tv4-' + result . error . code , false ) ;
22772285 error = result . error ;
2286+
2287+ // In Angular 1.3+ return the viewValue, otherwise we inadvertenly
2288+ // will trigger a 'parse' error.
2289+ // we will stop the model value from updating with our own $validator
2290+ // later.
2291+ if ( ngModel . $validators ) {
2292+ return viewValue ;
2293+ }
2294+ // Angular 1.2 on the other hand lacks $validators and don't add a 'parse' error.
22782295 return undefined ;
22792296 }
22802297 return viewValue ;
@@ -2294,7 +2311,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
22942311 } ) ;
22952312
22962313 [ '$validators' , '$asyncValidators' ] . forEach ( function ( attr ) {
2297- // Check if our version of angular has i , i.e. 1.3+
2314+ // Check if our version of angular has validators , i.e. 1.3+
22982315 if ( form [ attr ] && ngModel [ attr ] ) {
22992316 angular . forEach ( form [ attr ] , function ( fn , name ) {
23002317 ngModel [ attr ] [ name ] = fn ;
@@ -2303,17 +2320,41 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
23032320 } ) ;
23042321
23052322 // Get in last of the parses so the parsed value has the correct type.
2306- // We don't use $validators since we like to set different errors depeding tv4 error codes
2323+ // We don't use $validators since we like to set different errors depending tv4 error codes
23072324 ngModel . $parsers . push ( validate ) ;
23082325
2326+ // But we do use one custom validator in the case of Angular 1.3 to stop the model from
2327+ // updating if we've found an error.
2328+ if ( ngModel . $validators ) {
2329+ ngModel . $validators . schemaForm = function ( ) {
2330+ // Any error and we're out of here!
2331+ return ! Object . keys ( ngModel . $error ) . some ( function ( e ) { return e !== 'schemaForm' } ) ;
2332+ }
2333+ }
2334+
23092335 // Listen to an event so we can validate the input on request
23102336 scope . $on ( 'schemaFormValidate' , function ( ) {
2337+
2338+ // We set the viewValue to trigger parsers,
2339+ // since modelValue might be empty and validating just that
2340+ // might change an existing error to a "required" error message.
23112341 if ( ngModel . $setDirty ) {
2342+
23122343 // Angular 1.3+
23132344 ngModel . $setDirty ( ) ;
2314- validate ( ngModel . $modelValue ) ;
2345+ ngModel . $setViewValue ( ngModel . $viewValue ) ;
2346+ ngModel . $commitViewValue ( ) ;
2347+
2348+ // In Angular 1.3 setting undefined as a viewValue does not trigger parsers
2349+ // so we need to do a special required check. Fortunately we have $isEmpty
2350+ if ( form . required && ngModel . $isEmpty ( ) ) {
2351+ ngModel . $setValidity ( 'tv4-302' , false ) ;
2352+ }
2353+
23152354 } else {
23162355 // Angular 1.2
2356+ // In angular 1.2 setting a viewValue of undefined will trigger the parser.
2357+ // hence required works.
23172358 ngModel . $setViewValue ( ngModel . $viewValue ) ;
23182359 }
23192360
0 commit comments