@@ -541,20 +541,35 @@ angular.module('schemaForm').provider('schemaFormDecorators',
541
541
return ( expression && $interpolate ( expression ) ( locals ) ) ;
542
542
} ;
543
543
544
- //This works since we ot the ngModel from the array or the schema-validate directive.
544
+ //This works since we get the ngModel from the array or the schema-validate directive.
545
545
scope . hasSuccess = function ( ) {
546
546
if ( ! scope . ngModel ) {
547
547
return false ;
548
548
}
549
- return scope . ngModel . $valid &&
549
+ if ( scope . options && scope . options . pristine &&
550
+ scope . options . pristine . success === false ) {
551
+ return scope . ngModel . $valid &&
552
+ ! scope . ngModel . $pristine && ! scope . ngModel . $isEmpty ( scope . ngModel . $modelValue ) ;
553
+ } else {
554
+ return scope . ngModel . $valid &&
550
555
( ! scope . ngModel . $pristine || ! scope . ngModel . $isEmpty ( scope . ngModel . $modelValue ) ) ;
556
+ }
551
557
} ;
552
558
553
559
scope . hasError = function ( ) {
554
560
if ( ! scope . ngModel ) {
555
561
return false ;
556
562
}
557
- return scope . ngModel . $invalid && ! scope . ngModel . $pristine ;
563
+ if ( ! scope . options || ! scope . options . pristine || scope . options . pristine . errors !== false ) {
564
+ // Show errors in pristine forms. The default.
565
+ // Note that "validateOnRender" option defaults to *not* validate initial form.
566
+ // so as a default there won't be any error anyway, but if the model is modified
567
+ // from the outside the error will show even if the field is pristine.
568
+ return scope . ngModel . $invalid ;
569
+ } else {
570
+ // Don't show errors in pristine forms.
571
+ return scope . ngModel . $invalid && ! scope . ngModel . $pristine ;
572
+ }
558
573
} ;
559
574
560
575
/**
@@ -1809,11 +1824,27 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
1809
1824
scope . $on ( 'schemaFormValidate' , scope . validateArray ) ;
1810
1825
1811
1826
scope . hasSuccess = function ( ) {
1812
- return ngModel . $valid && ! ngModel . $pristine ;
1827
+ if ( scope . options && scope . options . pristine &&
1828
+ scope . options . pristine . success === false ) {
1829
+ return ngModel . $valid &&
1830
+ ! ngModel . $pristine && ! ngModel . $isEmpty ( ngModel . $modelValue ) ;
1831
+ } else {
1832
+ return ngModel . $valid &&
1833
+ ( ! ngModel . $pristine || ! ngModel . $isEmpty ( ngModel . $modelValue ) ) ;
1834
+ }
1813
1835
} ;
1814
1836
1815
1837
scope . hasError = function ( ) {
1816
- return ngModel . $invalid ;
1838
+ if ( ! scope . options || ! scope . options . pristine || scope . options . pristine . errors !== false ) {
1839
+ // Show errors in pristine forms. The default.
1840
+ // Note that "validateOnRender" option defaults to *not* validate initial form.
1841
+ // so as a default there won't be any error anyway, but if the model is modified
1842
+ // from the outside the error will show even if the field is pristine.
1843
+ return ngModel . $invalid ;
1844
+ } else {
1845
+ // Don't show errors in pristine forms.
1846
+ return ngModel . $invalid && ! ngModel . $pristine ;
1847
+ }
1817
1848
} ;
1818
1849
1819
1850
scope . schemaError = function ( ) {
@@ -1967,20 +1998,35 @@ angular.module('schemaForm').directive('sfField',
1967
1998
return ( expression && $interpolate ( expression ) ( locals ) ) ;
1968
1999
} ;
1969
2000
1970
- //This works since we ot the ngModel from the array or the schema-validate directive.
2001
+ //This works since we get the ngModel from the array or the schema-validate directive.
1971
2002
scope . hasSuccess = function ( ) {
1972
2003
if ( ! scope . ngModel ) {
1973
2004
return false ;
1974
2005
}
1975
- return scope . ngModel . $valid &&
2006
+ if ( scope . options && scope . options . pristine &&
2007
+ scope . options . pristine . success === false ) {
2008
+ return scope . ngModel . $valid &&
2009
+ ! scope . ngModel . $pristine && ! scope . ngModel . $isEmpty ( scope . ngModel . $modelValue ) ;
2010
+ } else {
2011
+ return scope . ngModel . $valid &&
1976
2012
( ! scope . ngModel . $pristine || ! scope . ngModel . $isEmpty ( scope . ngModel . $modelValue ) ) ;
2013
+ }
1977
2014
} ;
1978
2015
1979
2016
scope . hasError = function ( ) {
1980
2017
if ( ! scope . ngModel ) {
1981
2018
return false ;
1982
2019
}
1983
- return scope . ngModel . $invalid && ! scope . ngModel . $pristine ;
2020
+ if ( ! scope . options || ! scope . options . pristine || scope . options . pristine . errors !== false ) {
2021
+ // Show errors in pristine forms. The default.
2022
+ // Note that "validateOnRender" option defaults to *not* validate initial form.
2023
+ // so as a default there won't be any error anyway, but if the model is modified
2024
+ // from the outside the error will show even if the field is pristine.
2025
+ return scope . ngModel . $invalid ;
2026
+ } else {
2027
+ // Don't show errors in pristine forms.
2028
+ return scope . ngModel . $invalid && ! scope . ngModel . $pristine ;
2029
+ }
1984
2030
} ;
1985
2031
1986
2032
/**
@@ -2038,7 +2084,8 @@ angular.module('schemaForm').directive('sfField',
2038
2084
scope . $broadcast ( 'schemaFormValidate' ) ;
2039
2085
}
2040
2086
}
2041
- } ) ;
2087
+ }
2088
+ ) ;
2042
2089
2043
2090
// Clean up the model when the corresponding form field is $destroy-ed.
2044
2091
// Default behavior can be supplied as a globalOption, and behavior can be overridden
@@ -2105,60 +2152,80 @@ angular.module('schemaForm').directive('sfMessage',
2105
2152
scope . $watch ( attrs . sfMessage , function ( msg ) {
2106
2153
if ( msg ) {
2107
2154
message = $sanitize ( msg ) ;
2108
- if ( scope . ngModel ) {
2109
- update ( scope . ngModel . $valid ) ;
2110
- } else {
2111
- update ( ) ;
2112
- }
2155
+ update ( ! ! scope . ngModel ) ;
2113
2156
}
2114
2157
} ) ;
2115
2158
}
2116
2159
2117
- var update = function ( valid ) {
2118
- if ( valid && ! scope . hasError ( ) ) {
2119
- element . html ( message ) ;
2120
- } else {
2121
- var errors = [ ] ;
2122
- angular . forEach ( ( ( scope . ngModel && scope . ngModel . $error ) || { } ) , function ( status , code ) {
2123
- if ( status ) {
2124
- // if true then there is an error
2125
- // Angular 1.3 removes properties, so we will always just have errors.
2126
- // Angular 1.2 sets them to false.
2127
- errors . push ( code ) ;
2128
- }
2129
- } ) ;
2160
+ var currentMessage ;
2161
+ // Only call html() if needed.
2162
+ var setMessage = function ( msg ) {
2163
+ if ( msg !== currentMessage ) {
2164
+ element . html ( msg ) ;
2165
+ currentMessage = msg ;
2166
+ }
2167
+ } ;
2130
2168
2131
- // In Angular 1.3 we use one $validator to stop the model value from getting updated.
2132
- // this means that we always end up with a 'schemaForm' error.
2133
- errors = errors . filter ( function ( e ) { return e !== 'schemaForm' ; } ) ;
2134
-
2135
- // We only show one error.
2136
- // TODO: Make that optional
2137
- var error = errors [ 0 ] ;
2138
-
2139
- if ( error ) {
2140
- element . html ( sfErrorMessage . interpolate (
2141
- error ,
2142
- scope . ngModel . $modelValue ,
2143
- scope . ngModel . $viewValue ,
2144
- scope . form ,
2145
- scope . options && scope . options . validationMessage
2146
- ) ) ;
2169
+ var update = function ( checkForErrors ) {
2170
+ if ( checkForErrors ) {
2171
+ if ( ! scope . hasError ( ) ) {
2172
+ setMessage ( message ) ;
2147
2173
} else {
2148
- element . html ( message ) ;
2174
+ var errors = [ ] ;
2175
+ angular . forEach ( scope . ngModel && scope . ngModel . $error , function ( status , code ) {
2176
+ if ( status ) {
2177
+ // if true then there is an error
2178
+ // Angular 1.3 removes properties, so we will always just have errors.
2179
+ // Angular 1.2 sets them to false.
2180
+ errors . push ( code ) ;
2181
+ }
2182
+ } ) ;
2183
+
2184
+ // In Angular 1.3 we use one $validator to stop the model value from getting updated.
2185
+ // this means that we always end up with a 'schemaForm' error.
2186
+ errors = errors . filter ( function ( e ) { return e !== 'schemaForm' ; } ) ;
2187
+
2188
+ // We only show one error.
2189
+ // TODO: Make that optional
2190
+ var error = errors [ 0 ] ;
2191
+
2192
+ if ( error ) {
2193
+ setMessage ( sfErrorMessage . interpolate (
2194
+ error ,
2195
+ scope . ngModel . $modelValue ,
2196
+ scope . ngModel . $viewValue ,
2197
+ scope . form ,
2198
+ scope . options && scope . options . validationMessage
2199
+ ) ) ;
2200
+ } else {
2201
+ setMessage ( message ) ;
2202
+ }
2149
2203
}
2204
+ } else {
2205
+ setMessage ( message ) ;
2150
2206
}
2151
2207
} ;
2152
2208
2153
2209
// Update once.
2154
2210
update ( ) ;
2155
2211
2156
- scope . $watchCollection ( 'ngModel.$error' , function ( ) {
2157
- if ( scope . ngModel ) {
2158
- update ( scope . ngModel . $valid ) ;
2212
+ var once = scope . $watch ( 'ngModel' , function ( ngModel ) {
2213
+ if ( ngModel ) {
2214
+ // We also listen to changes of the model via parsers and formatters.
2215
+ // This is since both the error message can change and given a pristine
2216
+ // option to not show errors the ngModel.$error might not have changed
2217
+ // but we're not pristine any more so we should change!
2218
+ ngModel . $parsers . push ( function ( val ) { update ( true ) ; return val ; } ) ;
2219
+ ngModel . $formatters . push ( function ( val ) { update ( true ) ; return val ; } ) ;
2220
+ once ( ) ;
2159
2221
}
2160
2222
} ) ;
2161
2223
2224
+ // We watch for changes in $error
2225
+ scope . $watchCollection ( 'ngModel.$error' , function ( ) {
2226
+ update ( ! ! scope . ngModel ) ;
2227
+ } ) ;
2228
+
2162
2229
}
2163
2230
} ;
2164
2231
} ] ) ;
@@ -2619,11 +2686,13 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2619
2686
sfSelect ( path , scope . model , ngModel . $modelValue ) ;
2620
2687
} ) ;
2621
2688
} ) ;
2622
- }
2689
+ } ;
2690
+
2623
2691
2624
2692
// Validate against the schema.
2625
2693
2626
2694
var validate = function ( viewValue ) {
2695
+ //console.log('validate called', viewValue)
2627
2696
//Still might be undefined
2628
2697
if ( ! form ) {
2629
2698
return viewValue ;
@@ -2635,7 +2704,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2635
2704
}
2636
2705
2637
2706
var result = sfValidator . validate ( form , viewValue ) ;
2638
-
2707
+ //console.log('result is', result)
2639
2708
// Since we might have different tv4 errors we must clear all
2640
2709
// errors that start with tv4-
2641
2710
Object . keys ( ngModel . $error )
@@ -2690,6 +2759,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2690
2759
// updating if we've found an error.
2691
2760
if ( ngModel . $validators ) {
2692
2761
ngModel . $validators . schemaForm = function ( ) {
2762
+ //console.log('validators called.')
2693
2763
// Any error and we're out of here!
2694
2764
return ! Object . keys ( ngModel . $error ) . some ( function ( e ) { return e !== 'schemaForm' ; } ) ;
2695
2765
} ;
@@ -2733,6 +2803,20 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
2733
2803
}
2734
2804
} ;
2735
2805
2806
+ var first = true ;
2807
+ ngModel . $formatters . push ( function ( val ) {
2808
+
2809
+ // When a form first loads this will be called for each field.
2810
+ // we usually don't want that.
2811
+ if ( ngModel . $pristine && first &&
2812
+ ( ! scope . options || scope . options . validateOnRender !== true ) ) {
2813
+ first = false ;
2814
+ return val ;
2815
+ }
2816
+ validate ( ngModel . $modelValue ) ;
2817
+ return val ;
2818
+ } ) ;
2819
+
2736
2820
// Listen to an event so we can validate the input on request
2737
2821
scope . $on ( 'schemaFormValidate' , scope . validateField ) ;
2738
2822
0 commit comments