11/**
22 * Directive that handles the model arrays
33 */
4- angular . module ( 'schemaForm' ) . directive ( 'sfArray' , [ 'sfSelect' , 'schemaForm' ,
5- function ( sfSelect , schemaForm ) {
4+ angular . module ( 'schemaForm' ) . directive ( 'sfArray' , [ 'sfSelect' , 'schemaForm' , 'sfValidator' ,
5+ function ( sfSelect , schemaForm , sfValidator ) {
66
77 var setIndex = function ( index ) {
88 return function ( form ) {
@@ -15,7 +15,8 @@ function(sfSelect, schemaForm) {
1515 return {
1616 restrict : 'A' ,
1717 scope : true ,
18- link : function ( scope , element , attrs ) {
18+ require : '?ngModel' ,
19+ link : function ( scope , element , attrs , ngModel ) {
1920 var formDefCache = { } ;
2021
2122 // Watch for the form definition and then rewrite it.
@@ -74,10 +75,10 @@ function(sfSelect, schemaForm) {
7475 } ) ;
7576
7677 // If there are no defaults nothing is added so we need to initialize
77- // the array. null for basic values, {} or [] for the others.
78+ // the array. undefined for basic values, {} or [] for the others.
7879 if ( len === list . length ) {
7980 var type = sfSelect ( 'schema.items.type' , form ) ;
80- var dflt = null ;
81+ var dflt ;
8182 if ( type === 'object' ) {
8283 dflt = { } ;
8384 } else if ( type === 'array' ) {
@@ -107,9 +108,6 @@ function(sfSelect, schemaForm) {
107108 if ( form . titleMap && form . titleMap . length > 0 ) {
108109 scope . titleMapValues = [ ] ;
109110
110-
111-
112-
113111 // We watch the model for changes and the titleMapValues to reflect
114112 // the modelArray
115113 var updateTitleMapValues = function ( arr ) {
@@ -146,6 +144,56 @@ function(sfSelect, schemaForm) {
146144 } ) ;
147145 }
148146
147+
148+ // If there is a ngModel present we need to validate when asked.
149+ if ( ngModel ) {
150+ var error ;
151+
152+ // Listen to an event so we can validate the input on request
153+ scope . $on ( 'schemaFormValidate' , function ( payload ) {
154+ // The actual content of the array is validated by each field
155+ // so we settle for checking validations specific to arrays
156+
157+ // Since we prefill with empty arrays we can get the funny situation
158+ // where the array is required but empty in the gui but still validates.
159+ // Thats why we check the length.
160+ var result = sfValidator . validate (
161+ form ,
162+ scope . modelArray . length > 0 ? scope . modelArray : undefined
163+ ) ;
164+ console . log ( result . error )
165+ if ( result . valid === false &&
166+ result . error &&
167+ ( result . error . dataPath === '' ||
168+ result . error . dataPath === '/' + form . key [ form . key . length - 1 ] ) ) {
169+ console . log ( 'setting invlid' )
170+ // Set viewValue to trigger $dirty on field. If someone knows a
171+ // a better way to do it please tell.
172+ ngModel . $setViewValue ( scope . modelArray ) ;
173+ error = result . error ;
174+ ngModel . $setValidity ( 'schema' , false ) ;
175+
176+ } else {
177+ ngModel . $setValidity ( 'schema' , true ) ;
178+ }
179+
180+ } ) ;
181+
182+
183+ scope . hasSuccess = function ( ) {
184+ return ngModel . $valid && ! ngModel . $pristine ;
185+ } ;
186+
187+ scope . hasError = function ( ) {
188+ return ngModel . $invalid ;
189+ } ;
190+
191+ scope . schemaError = function ( ) {
192+ return error ;
193+ } ;
194+
195+ }
196+
149197 once ( ) ;
150198 } ) ;
151199 }
0 commit comments