@@ -4,20 +4,20 @@ div.vue-form-generator(v-if='schema != null')
44 div( v-html ="errors" )
55 fieldset( v-if ="schema.fields" , :is ='tag' )
66 template( v-for ='field in fields' )
7- form-group( v-if ='fieldVisible(field)' , :field ="field" , :errors ="errors" , :model ="model" , :options ="options" )
7+ form-group( v-if ='fieldVisible(field)' , :field ="field" , :errors ="errors" , :model ="model" , :options ="options" , :eventBus = "eventBus" )
88
99 template( v-for ='group in groups' )
1010 fieldset( :is ='tag' , :class ='getFieldRowClasses(group)' )
1111 legend( v-if ='group.legend' ) {{ group.legend }}
1212 template( v-for ='field in group.fields' )
13- form-group( v-if ='fieldVisible(field)' , :field ="field" , :errors ="errors" , :model ="model" , :options ="options" )
13+ form-group( v-if ='fieldVisible(field)' , :field ="field" , :errors ="errors" , :model ="model" , :options ="options" , :eventBus = "eventBus" )
1414</template >
1515
1616<script >
17+ import Vue from " vue" ;
1718import { get as objGet , forEach , isFunction , isNil , isArray } from " lodash" ;
1819import formMixin from " ./formMixin.js" ;
1920import formGroup from " ./formGroup.vue" ;
20- import { eventBus } from " ./event-bus.js" ;
2121
2222export default {
2323 name: " formGenerator" ,
@@ -61,7 +61,9 @@ export default {
6161 },
6262
6363 data () {
64+ const eventBus = new Vue ();
6465 return {
66+ eventBus: eventBus,
6567 totalNumberOfFields: 0 ,
6668 errors: [] // Validation errors
6769 };
@@ -157,9 +159,9 @@ export default {
157159
158160 let formErrors = [];
159161
160- eventBus .$on (" field-deregistering" , () => {
162+ this . eventBus .$on (" field-deregistering" , () => {
161163 console .warn (" Fields were deleted during validation process" );
162- eventBus .$emit (" fields-validation-terminated" , formErrors);
164+ this . eventBus .$emit (" fields-validation-terminated" , formErrors);
163165 reject (formErrors);
164166 });
165167
@@ -175,14 +177,14 @@ export default {
175177 }
176178
177179 if (fieldsValidated === this .totalNumberOfFields ) {
178- eventBus .$off (" field-validated" , counter);
180+ this . eventBus .$off (" field-validated" , counter);
179181 if (objGet (this .options , " validateAfterChanged" , false )) {
180- eventBus .$on (" field-validated" , this .onFieldValidated );
182+ this . eventBus .$on (" field-validated" , this .onFieldValidated );
181183 }
182184 this .errors = formErrors;
183185 let isValid = formErrors .length === 0 ;
184186 this .$emit (" validated" , isValid, formErrors, this );
185- eventBus .$emit (" fields-validation-terminated" , formErrors);
187+ this . eventBus .$emit (" fields-validation-terminated" , formErrors);
186188
187189 if (isValid) {
188190 resolve ();
@@ -192,39 +194,39 @@ export default {
192194 }
193195 };
194196 if (objGet (this .options , " validateAfterChanged" , false )) {
195- eventBus .$off (" field-validated" , this .onFieldValidated );
197+ this . eventBus .$off (" field-validated" , this .onFieldValidated );
196198 }
197- eventBus .$on (" field-validated" , counter);
198- eventBus .$emit (" validate-fields" , this );
199+ this . eventBus .$on (" field-validated" , counter);
200+ this . eventBus .$emit (" validate-fields" , this );
199201 });
200202 },
201203
202204 // Clear validation errors
203205 clearValidationErrors () {
204206 this .errors .splice (0 );
205- eventBus .$emit (" clear-validation-errors" , this .clearValidationErrors );
207+ this . eventBus .$emit (" clear-validation-errors" , this .clearValidationErrors );
206208 }
207209 },
208210
209211 created () {
210212 if (objGet (this .options , " validateAfterChanged" , false )) {
211- eventBus .$on (" field-validated" , this .onFieldValidated );
213+ this . eventBus .$on (" field-validated" , this .onFieldValidated );
212214 }
213- eventBus .$on (" model-updated" , this .onModelUpdated );
214- eventBus .$on (" fields-validation-trigger" , this .validate );
215- eventBus .$on (" field-registering" , () => {
215+ this . eventBus .$on (" model-updated" , this .onModelUpdated );
216+ this . eventBus .$on (" fields-validation-trigger" , this .validate );
217+ this . eventBus .$on (" field-registering" , () => {
216218 this .totalNumberOfFields = this .totalNumberOfFields + 1 ;
217219 });
218- eventBus .$on (" field-deregistering" , () => {
220+ this . eventBus .$on (" field-deregistering" , () => {
219221 this .totalNumberOfFields = this .totalNumberOfFields - 1 ;
220222 });
221223 },
222224 beforeDestroy () {
223- eventBus .$off (" field-validated" );
224- eventBus .$off (" model-updated" );
225- eventBus .$off (" fields-validation-trigger" );
226- eventBus .$off (" field-registering" );
227- eventBus .$off (" field-deregistering" );
225+ this . eventBus .$off (" field-validated" );
226+ this . eventBus .$off (" model-updated" );
227+ this . eventBus .$off (" fields-validation-trigger" );
228+ this . eventBus .$off (" field-registering" );
229+ this . eventBus .$off (" field-deregistering" );
228230 }
229231};
230232 </script >
0 commit comments