1
+ ( function ( root , factory ) {
2
+ if ( typeof define === 'function' && define . amd ) {
3
+ define ( [ 'angular' , 'ObjectPath' , 'tv4' ] , factory ) ;
4
+ } else if ( typeof exports === 'object' ) {
5
+ module . exports = factory ( require ( 'angular' ) , require ( 'ObjectPath' ) , require ( 'tv4' ) ) ;
6
+ } else {
7
+ root . schemaForm = factory ( root . angular , root . ObjectPath , root . tv4 ) ;
8
+ }
9
+ } ( this , function ( angular , ObjectPath , tv4 ) {
1
10
// Deps is sort of a problem for us, maybe in the future we will ask the user to depend
2
11
// on modules for add-ons
3
12
20
29
deps . push ( 'angularSpectrumColorpicker' ) ;
21
30
} catch ( e ) { }
22
31
23
- angular . module ( 'schemaForm' , deps ) ;
32
+ var schemaForm = angular . module ( 'schemaForm' , deps ) ;
24
33
25
34
angular . module ( 'schemaForm' ) . provider ( 'sfPath' ,
26
35
[ function ( ) {
@@ -159,8 +168,8 @@ angular.module('schemaForm').provider('schemaFormDecorators',
159
168
160
169
var createDirective = function ( name ) {
161
170
$compileProvider . directive ( name ,
162
- [ '$parse' , '$compile' , '$http' , '$templateCache' , '$interpolate' , '$q' , 'sfErrorMessage' ,
163
- function ( $parse , $compile , $http , $templateCache , $interpolate , $q , sfErrorMessage ) {
171
+ [ '$parse' , '$compile' , '$http' , '$templateCache' , '$interpolate' , '$q' , 'sfErrorMessage' , 'sfPath' ,
172
+ function ( $parse , $compile , $http , $templateCache , $interpolate , $q , sfErrorMessage , sfPath ) {
164
173
165
174
return {
166
175
restrict : 'AE' ,
@@ -287,6 +296,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
287
296
return sfErrorMessage . interpolate (
288
297
( schemaError && schemaError . code + '' ) || 'default' ,
289
298
( scope . ngModel && scope . ngModel . $modelValue ) || '' ,
299
+ ( scope . ngModel && scope . ngModel . $viewValue ) || '' ,
290
300
scope . form ,
291
301
scope . options && scope . options . validationMessage
292
302
) ;
@@ -331,18 +341,23 @@ angular.module('schemaForm').provider('schemaFormDecorators',
331
341
// Do we have a condition? Then we slap on an ng-if on all children,
332
342
// but be nice to existing ng-if.
333
343
if ( form . condition ) {
344
+
345
+ var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})' ;
346
+ if ( form . key ) {
347
+ evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "modelValue": model' + sfPath . stringify ( form . key ) + '})' ;
348
+ }
349
+
334
350
angular . forEach ( element . children ( ) , function ( child ) {
335
351
var ngIf = child . getAttribute ( 'ng-if' ) ;
336
352
child . setAttribute (
337
353
'ng-if' ,
338
354
ngIf ?
339
355
'(' + ngIf +
340
- ') || (evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex }) )'
341
- : ' evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex })'
356
+ ') || (' + evalExpr + ' )'
357
+ : evalExpr
342
358
) ;
343
359
} ) ;
344
360
}
345
-
346
361
$compile ( element . contents ( ) ) ( scope ) ;
347
362
} ) ;
348
363
@@ -372,15 +387,16 @@ angular.module('schemaForm').provider('schemaFormDecorators',
372
387
if ( ! form . validationMessage ) {
373
388
form . validationMessage = { } ;
374
389
}
375
- console . log ( 'settings validationMessage' , validationMessage )
376
390
form . validationMessage [ error ] = validationMessage ;
377
391
}
378
392
379
393
scope . ngModel . $setValidity ( error , validity === true ) ;
380
394
381
- // Setting or removing a validity can change the field to believe its valid
382
- // but its not. So lets trigger its validation as well.
383
- scope . $broadcast ( 'schemaFormValidate' ) ;
395
+ if ( validity === true ) {
396
+ // Setting or removing a validity can change the field to believe its valid
397
+ // but its not. So lets trigger its validation as well.
398
+ scope . $broadcast ( 'schemaFormValidate' ) ;
399
+ }
384
400
}
385
401
} )
386
402
}
@@ -548,32 +564,32 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
548
564
// TODO: Humanize these.
549
565
var defaultMessages = {
550
566
'default' : 'Field does not validate' ,
551
- 0 : 'Invalid type, expected {{schema.type}}) ' ,
567
+ 0 : 'Invalid type, expected {{schema.type}}' ,
552
568
1 : 'No enum match for: {{value}}' ,
553
569
10 : 'Data does not match any schemas from "anyOf"' ,
554
570
11 : 'Data does not match any schemas from "oneOf"' ,
555
571
12 : 'Data is valid against more than one schema from "oneOf"' ,
556
572
13 : 'Data matches schema from "not"' ,
557
573
// Numeric errors
558
- 100 : 'Value {{value}} is not a multiple of {{schema.multipleOf }}' ,
559
- 101 : 'Value {{value }} is less than minimum {{schema.minimum}}' ,
560
- 102 : 'Value {{value }} is equal to exclusive minimum {{schema.minimum}}' ,
561
- 103 : 'Value {{value }} is greater than maximum {{schema.maximum}}' ,
562
- 104 : 'Value {{value }} is equal to exclusive maximum {{schema.maximum}}' ,
563
- 105 : 'Value {{value}} is not a valid number' ,
574
+ 100 : 'Value is not a multiple of {{schema.divisibleBy }}' ,
575
+ 101 : '{{viewValue }} is less than the allowed minimum of {{schema.minimum}}' ,
576
+ 102 : '{{viewValue }} is equal to the exclusive minimum {{schema.minimum}}' ,
577
+ 103 : '{{viewValue }} is greater than the allowed maximum of {{schema.maximum}}' ,
578
+ 104 : '{{viewValue }} is equal to the exclusive maximum {{schema.maximum}}' ,
579
+ 105 : 'Value is not a valid number' ,
564
580
// String errors
565
- 200 : 'String is too short ({{value .length}} chars), minimum {{schema.minimum }}' ,
566
- 201 : 'String is too long ({{value .length}} chars), maximum {{schema.maximum }}' ,
581
+ 200 : 'String is too short ({{viewValue .length}} chars), minimum {{schema.minLength }}' ,
582
+ 201 : 'String is too long ({{viewValue .length}} chars), maximum {{schema.maxLength }}' ,
567
583
202 : 'String does not match pattern: {{schema.pattern}}' ,
568
584
// Object errors
569
- 300 : 'Too few properties defined, minimum {{schema.minimum }}' ,
570
- 301 : 'Too many properties defined, maximum {{schema.maximum }}' ,
585
+ 300 : 'Too few properties defined, minimum {{schema.minProperties }}' ,
586
+ 301 : 'Too many properties defined, maximum {{schema.maxProperties }}' ,
571
587
302 : 'Required' ,
572
588
303 : 'Additional properties not allowed' ,
573
589
304 : 'Dependency failed - key must exist' ,
574
590
// Array errors
575
- 400 : 'Array is too short ({{value.length}}), minimum {{schema.minimum }}' ,
576
- 401 : 'Array is too long ({{value.length}}), maximum {{schema.maximum }}' ,
591
+ 400 : 'Array is too short ({{value.length}}), minimum {{schema.maxItems }}' ,
592
+ 401 : 'Array is too long ({{value.length}}), maximum {{schema.minItems }}' ,
577
593
402 : 'Array items are not unique' ,
578
594
403 : 'Additional items not allowed' ,
579
595
// Format errors
@@ -585,6 +601,15 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
585
601
1000 : 'Unknown property (not in schema)'
586
602
} ;
587
603
604
+ // In some cases we get hit with an angular validation error
605
+ defaultMessages . number = defaultMessages [ 105 ] ;
606
+ defaultMessages . required = defaultMessages [ 302 ] ;
607
+ defaultMessages . min = defaultMessages [ 101 ] ;
608
+ defaultMessages . max = defaultMessages [ 103 ] ;
609
+ defaultMessages . maxlength = defaultMessages [ 201 ] ;
610
+ defaultMessages . minlength = defaultMessages [ 200 ] ;
611
+ defaultMessages . pattern = defaultMessages [ 202 ] ;
612
+
588
613
this . setDefaultMessages = function ( messages ) {
589
614
defaultMessages = messages ;
590
615
} ;
@@ -609,12 +634,13 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
609
634
* @param {string } error the error code, i.e. tv4-xxx for tv4 errors, otherwise it's whats on
610
635
* ngModel.$error for custom errors.
611
636
* @param {Any } value the actual model value.
637
+ * @param {Any } viewValue the viewValue
612
638
* @param {Object } form a form definition object for this field
613
639
* @param {Object } global the global validation messages object (even though its called global
614
640
* its actually just shared in one instance of sf-schema)
615
641
* @return {string } The error message.
616
642
*/
617
- service . interpolate = function ( error , value , form , global ) {
643
+ service . interpolate = function ( error , value , viewValue , form , global ) {
618
644
global = global || { } ;
619
645
var validationMessage = form . validationMessage || { } ;
620
646
@@ -640,6 +666,7 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
640
666
var context = {
641
667
error : error ,
642
668
value : value ,
669
+ viewValue : viewValue ,
643
670
form : form ,
644
671
schema : form . schema ,
645
672
title : form . title || ( form . schema && form . schema . title )
@@ -1495,7 +1522,7 @@ angular.module('schemaForm').directive('sfMessage',
1495
1522
}
1496
1523
1497
1524
var update = function ( valid ) {
1498
- if ( valid && scope . hasError ( ) ) {
1525
+ if ( valid && ! scope . hasError ( ) ) {
1499
1526
element . html ( msg ) ;
1500
1527
} else {
1501
1528
@@ -1505,24 +1532,13 @@ angular.module('schemaForm').directive('sfMessage',
1505
1532
1506
1533
// We only show one error.
1507
1534
// TODO: Make that optional
1508
- // tv4- errors take precedence
1509
1535
var error = errors [ 0 ] ;
1510
- if ( errors . length > 1 ) {
1511
-
1512
- error = errors . reduce ( function ( prev , value ) {
1513
- if ( prev && prev . indexOf ( 'tv4-' ) === 0 ) {
1514
- return prev ;
1515
- }
1516
- return value ;
1517
- } ) ;
1518
- console . log ( 'reduced' , errors , error )
1519
-
1520
- }
1521
1536
1522
1537
if ( error ) {
1523
1538
element . html ( sfErrorMessage . interpolate (
1524
1539
error ,
1525
1540
scope . ngModel . $modelValue ,
1541
+ scope . ngModel . $viewValue ,
1526
1542
scope . form ,
1527
1543
scope . options && scope . options . validationMessage
1528
1544
) ) ;
@@ -1752,6 +1768,11 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
1752
1768
return viewValue ;
1753
1769
}
1754
1770
1771
+ // Omit TV4 validation
1772
+ if ( scope . options && scope . options . tv4Validation === false ) {
1773
+ return viewValue ;
1774
+ }
1775
+
1755
1776
var result = sfValidator . validate ( form , viewValue ) ;
1756
1777
// Since we might have different tv4 errors we must clear all
1757
1778
// errors that start with tv4-
@@ -1814,3 +1835,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
1814
1835
}
1815
1836
} ;
1816
1837
} ] ) ;
1838
+
1839
+ return schemaForm ;
1840
+ } ) ) ;
0 commit comments