@@ -19,7 +19,7 @@ const componentDef = {
19
19
hideNestedForm : false ,
20
20
nestedFormRuntimeTemplates : { } ,
21
21
nestedFormRuntimeTemplateDomElements : { } ,
22
- deletedNestedNewInstanceFormsCount : { } ,
22
+ deletedNestedForms : { } ,
23
23
nestedFormRuntimeId : ""
24
24
} ;
25
25
} ,
@@ -69,18 +69,28 @@ const componentDef = {
69
69
setErrorKey : function ( key , value ) {
70
70
Vue . set ( this . errors , key , value ) ;
71
71
} ,
72
+ flushErrors : function ( key , value ) {
73
+ this . errors = { } ;
74
+ } ,
72
75
setNestedFormsError : function ( errors ) {
73
76
let self = this ;
74
77
Object . keys ( errors ) . forEach ( function ( errorKey ) {
75
78
if ( errorKey . includes ( "." ) ) {
76
79
let childErrorKey = errorKey . split ( "." ) [ 1 ]
77
80
let childModelName = errorKey . split ( "." ) [ 0 ] . split ( "[" ) [ 0 ]
78
81
let childModelIndex = errorKey . split ( "." ) [ 0 ] . split ( "[" ) [ 1 ] . split ( "]" ) [ 0 ]
79
- childModelIndex = parseInt ( childModelIndex ) + parseInt ( self . deletedNestedNewInstanceFormsCount [ childModelName + "_attributes" ] )
80
- self . nestedForms [ childModelName + "_attributes" ] [ childModelIndex ] . setErrorKey ( childErrorKey , errors [ errorKey ] )
82
+ let mappedChildModelIndex = self . mapToNestedForms ( parseInt ( childModelIndex ) , childModelName + "_attributes" )
83
+ self . nestedForms [ childModelName + "_attributes" ] [ mappedChildModelIndex ] . setErrorKey ( childErrorKey , errors [ errorKey ] )
81
84
}
82
85
} )
83
86
} ,
87
+ mapToNestedForms : function ( serverIndex , nestedFormKey ) {
88
+ var index = serverIndex ;
89
+ while ( this . deletedNestedForms [ nestedFormKey ] . includes ( index ) ) {
90
+ index ++ ;
91
+ }
92
+ return index ;
93
+ } ,
84
94
resetNestedForms : function ( ) {
85
95
var self = this ;
86
96
Object . keys ( self . nestedForms ) . forEach ( function ( childModelKey ) {
@@ -89,6 +99,7 @@ const componentDef = {
89
99
var destroyed = true ;
90
100
}
91
101
nestedFormInstance . setProps ( nestedFormInstance . data , null )
102
+ nestedFormInstance . initValues ( )
92
103
Vue . set ( nestedFormInstance . data )
93
104
if ( destroyed ) {
94
105
nestedFormInstance . hideNestedForm = true
@@ -106,9 +117,10 @@ const componentDef = {
106
117
} else {
107
118
primaryKey = id ;
108
119
}
109
- if ( this . data [ primaryKey ] == null ) {
110
- this . $parent . deletedNestedNewInstanceFormsCount [ this . props [ "fields_for" ] ] ++ ;
111
- }
120
+ // if(this.data[primaryKey] == null){
121
+ var id = parseInt ( this . nestedFormRuntimeId . replace ( "_" + this . props [ "fields_for" ] + "_child_" , "" ) ) ;
122
+ this . $parent . deletedNestedForms [ this . props [ "fields_for" ] ] . push ( id ) ;
123
+ // }
112
124
} ,
113
125
addItem : function ( key ) {
114
126
var templateString = JSON . parse ( this . $el . querySelector ( '#prototype-template-for-' + key ) . dataset [ ":template" ] )
@@ -121,14 +133,14 @@ const componentDef = {
121
133
} else {
122
134
existingItemsCount = this . nestedForms [ key ] . length
123
135
}
124
- dom_elem . querySelector ( '.matestack-form-fields-for' ) . id = "child- "+ existingItemsCount
136
+ dom_elem . querySelector ( '.matestack-form-fields-for' ) . id = key + "_child_ "+ existingItemsCount
125
137
Vue . set ( this . nestedFormRuntimeTemplateDomElements , key , dom_elem )
126
138
Vue . set ( this . nestedFormRuntimeTemplates , key , this . nestedFormRuntimeTemplateDomElements [ key ] . outerHTML )
127
139
} else {
128
140
var dom_elem = document . createElement ( 'div' )
129
141
dom_elem . innerHTML = templateString
130
142
var existingItemsCount = this . nestedForms [ key ] . length
131
- dom_elem . querySelector ( '.matestack-form-fields-for' ) . id = "child- "+ existingItemsCount
143
+ dom_elem . querySelector ( '.matestack-form-fields-for' ) . id = key + "_child_ "+ existingItemsCount
132
144
this . nestedFormRuntimeTemplateDomElements [ key ] . insertAdjacentHTML (
133
145
'beforeend' ,
134
146
dom_elem . innerHTML
@@ -289,6 +301,8 @@ const componentDef = {
289
301
return ;
290
302
}
291
303
304
+ self . flushErrors ( ) ;
305
+
292
306
if ( self . shouldResetFormOnSuccessfulSubmit ( ) )
293
307
{
294
308
self . setProps ( self . data , null ) ;
@@ -360,9 +374,6 @@ const componentDef = {
360
374
if ( this . props [ "fields_for" ] != undefined ) {
361
375
this . isNestedForm = true ;
362
376
363
- var id = parseInt ( self . $el . id . replace ( "child-" , "" ) ) ;
364
- this . nestedFormRuntimeId = Math . floor ( Math . random ( ) * 1000 ) ; ;
365
-
366
377
this . data = { "_destroy" : false } ;
367
378
368
379
//initialize nestedForm data in parent form if required
@@ -372,19 +383,25 @@ const componentDef = {
372
383
if ( this . $parent . nestedForms [ this . props [ "fields_for" ] ] == undefined ) {
373
384
this . $parent . nestedForms [ this . props [ "fields_for" ] ] = [ ] ;
374
385
}
375
- if ( this . $parent . deletedNestedNewInstanceFormsCount [ this . props [ "fields_for" ] ] == undefined ) {
376
- this . $parent . deletedNestedNewInstanceFormsCount [ this . props [ "fields_for" ] ] = 0
386
+ if ( this . $parent . deletedNestedForms [ this . props [ "fields_for" ] ] == undefined ) {
387
+ this . $parent . deletedNestedForms [ this . props [ "fields_for" ] ] = [ ]
377
388
}
378
389
390
+ var id = parseInt ( self . $el . id . replace ( this . props [ "fields_for" ] + "_child_" , "" ) ) ;
391
+
379
392
//setup data binding for serverside rendered nested forms
380
393
if ( isNaN ( id ) ) {
394
+ id = this . $parent . nestedForms [ this . props [ "fields_for" ] ] . length
395
+ this . nestedFormRuntimeId = "_" + this . props [ "fields_for" ] + "_child_" + id
396
+ this . $el . id = this . props [ "fields_for" ] + "_child_" + id
381
397
this . initValues ( )
382
398
this . $parent . data [ this . props [ "fields_for" ] ] . push ( this . data ) ;
383
399
this . $parent . nestedForms [ this . props [ "fields_for" ] ] . push ( this ) ;
384
400
}
385
401
386
402
//setup data binding for runtime nested forms (dynamic add via v-runtime-template)
387
403
if ( ! isNaN ( id ) ) {
404
+ this . nestedFormRuntimeId = "_" + this . props [ "fields_for" ] + "_child_" + id
388
405
if ( this . $parent . data [ this . props [ "fields_for" ] ] [ id ] == undefined ) {
389
406
//new runtime form
390
407
this . initValues ( )
@@ -402,8 +419,8 @@ const componentDef = {
402
419
let childErrorKey = errorKey . split ( "." ) [ 1 ]
403
420
let childModelName = errorKey . split ( "." ) [ 0 ] . split ( "[" ) [ 0 ]
404
421
let childModelIndex = errorKey . split ( "." ) [ 0 ] . split ( "[" ) [ 1 ] . split ( "]" ) [ 0 ]
405
- childModelIndex = parseInt ( childModelIndex ) + parseInt ( self . $parent . deletedNestedNewInstanceFormsCount [ childModelName + "_attributes" ] )
406
- if ( childModelName + "_attributes" == self . props [ "fields_for" ] && childModelIndex == id ) {
422
+ let mappedChildModelIndex = self . $parent . mapToNestedForms ( parseInt ( childModelIndex ) , childModelName + "_attributes" )
423
+ if ( childModelName + "_attributes" == self . props [ "fields_for" ] && mappedChildModelIndex == id ) {
407
424
self . setErrorKey ( childErrorKey , self . $parent . errors [ errorKey ] )
408
425
}
409
426
}
0 commit comments