@@ -271,7 +271,6 @@ var transclusion = function() {
271
271
* Builds a form from a canonical form definition
272
272
*/
273
273
build : function ( form , decorator , slots ) {
274
- console . warn ( slots )
275
274
return build ( form , decorator , function ( url ) {
276
275
return $templateCache . get ( url ) || '' ;
277
276
} , slots ) ;
@@ -748,9 +747,9 @@ angular.module('schemaForm').provider('schemaFormDecorators',
748
747
this . addMapping = function ( name , type , url , builder , replace ) {
749
748
if ( decorators [ name ] ) {
750
749
decorators [ name ] [ type ] = {
751
- temlpate : url ,
750
+ template : url ,
752
751
builder : builder ,
753
- replace : replace
752
+ replace : ! ! replace
754
753
} ;
755
754
}
756
755
} ;
@@ -2029,13 +2028,20 @@ angular.module('schemaForm').directive('sfMessage',
2029
2028
element . html ( msg ) ;
2030
2029
} else {
2031
2030
2032
- var errors = Object . keys (
2033
- ( scope . ngModel && scope . ngModel . $error ) || { }
2034
- ) ;
2035
2031
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' ; } ) ;
2039
2045
2040
2046
// We only show one error.
2041
2047
// TODO: Make that optional
@@ -2265,6 +2271,8 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2265
2271
}
2266
2272
2267
2273
var result = sfValidator . validate ( form , viewValue ) ;
2274
+
2275
+
2268
2276
// Since we might have different tv4 errors we must clear all
2269
2277
// errors that start with tv4-
2270
2278
Object . keys ( ngModel . $error )
@@ -2275,6 +2283,15 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2275
2283
// it is invalid, return undefined (no model update)
2276
2284
ngModel . $setValidity ( 'tv4-' + result . error . code , false ) ;
2277
2285
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.
2278
2295
return undefined ;
2279
2296
}
2280
2297
return viewValue ;
@@ -2294,7 +2311,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2294
2311
} ) ;
2295
2312
2296
2313
[ '$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+
2298
2315
if ( form [ attr ] && ngModel [ attr ] ) {
2299
2316
angular . forEach ( form [ attr ] , function ( fn , name ) {
2300
2317
ngModel [ attr ] [ name ] = fn ;
@@ -2303,17 +2320,41 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2303
2320
} ) ;
2304
2321
2305
2322
// 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
2307
2324
ngModel . $parsers . push ( validate ) ;
2308
2325
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
+
2309
2335
// Listen to an event so we can validate the input on request
2310
2336
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.
2311
2341
if ( ngModel . $setDirty ) {
2342
+
2312
2343
// Angular 1.3+
2313
2344
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
+
2315
2354
} else {
2316
2355
// Angular 1.2
2356
+ // In angular 1.2 setting a viewValue of undefined will trigger the parser.
2357
+ // hence required works.
2317
2358
ngModel . $setViewValue ( ngModel . $viewValue ) ;
2318
2359
}
2319
2360
0 commit comments