1
- import { get as objGet , forEach , isFunction , isString , isArray , debounce , isNil } from "lodash" ;
1
+ import { get as objGet , forEach , isFunction , isString , isArray , debounce , isNil , uniqueId } from "lodash" ;
2
2
import validators from "../utils/validators" ;
3
- import { slugifyFormID } from "../utils/schema" ;
4
3
5
4
const convertValidator = ( validator ) => {
6
5
if ( isString ( validator ) ) {
@@ -37,11 +36,16 @@ export default {
37
36
} ,
38
37
eventBus : {
39
38
type : Object
39
+ } ,
40
+ fieldID : {
41
+ type : String
40
42
}
41
43
} ,
42
44
43
45
data ( ) {
46
+ const fieldUID = uniqueId ( this . fieldID + "_" ) ;
44
47
return {
48
+ fieldUID,
45
49
errors : [ ] ,
46
50
debouncedValidateFunc : null ,
47
51
debouncedFormatFunction : null
@@ -109,19 +113,21 @@ export default {
109
113
110
114
watch : {
111
115
errors : {
112
- handler : function ( errors ) {
116
+ handler ( errors ) {
113
117
this . $emit ( "errors-updated" , errors ) ;
114
118
}
115
119
}
116
120
} ,
117
121
118
122
methods : {
119
123
getValueFromOption ( field , option , defaultValue ) {
120
- if ( typeof this . $parent . getValueFromOption === "function" ) {
124
+ if ( isFunction ( this . $parent . getValueFromOption ) ) {
121
125
return this . $parent . getValueFromOption ( field , option , defaultValue ) ;
122
126
} else {
123
127
// Environnement de test ?
124
- if ( isNil ( field [ option ] ) ) return defaultValue ;
128
+ if ( isNil ( field [ option ] ) ) {
129
+ return defaultValue ;
130
+ }
125
131
126
132
return field [ option ] ;
127
133
}
@@ -142,12 +148,12 @@ export default {
142
148
if ( ! isArray ( this . schema . validator ) ) {
143
149
validators . push ( convertValidator ( this . schema . validator ) . bind ( this ) ) ;
144
150
} else {
145
- forEach ( this . schema . validator , ( validator ) => {
151
+ this . schema . validator . forEach ( ( validator ) => {
146
152
validators . push ( convertValidator ( validator ) . bind ( this ) ) ;
147
153
} ) ;
148
154
}
149
155
150
- forEach ( validators , ( validator ) => {
156
+ validators . forEach ( ( validator ) => {
151
157
if ( validateAsync ) {
152
158
results . push ( validator ( this . value , this . schema , this . model ) ) ;
153
159
} else {
@@ -167,7 +173,7 @@ export default {
167
173
168
174
let handleErrors = ( errors ) => {
169
175
let fieldErrors = [ ] ;
170
- forEach ( errors , ( err ) => {
176
+ errors . forEach ( ( err ) => {
171
177
if ( isArray ( err ) && err . length > 0 ) {
172
178
fieldErrors = fieldErrors . concat ( err ) ;
173
179
} else if ( isString ( err ) ) {
@@ -182,7 +188,7 @@ export default {
182
188
183
189
this . errors = fieldErrors ;
184
190
185
- this . eventBus . $emit ( "field-validated" , isValid , fieldErrors , this . _uid ) ;
191
+ this . eventBus . $emit ( "field-validated" , isValid , fieldErrors , this . fieldUID ) ;
186
192
return fieldErrors ;
187
193
} ;
188
194
@@ -265,11 +271,6 @@ export default {
265
271
}
266
272
} ,
267
273
268
- getFieldID ( schema ) {
269
- const idPrefix = objGet ( this . formOptions , "fieldIdPrefix" , "" ) ;
270
- return slugifyFormID ( schema , idPrefix ) ;
271
- } ,
272
-
273
274
formatValueToField ( value ) {
274
275
return value ;
275
276
} ,
0 commit comments