FormUtils.js
8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {post} from '../../../utils/fetch';
import {getApproversByRecord} from '../../../utils/formUtil';
/*check approvers from values*/
export const getApproversByValues=(service,record_data,form_name)=>{
return getApproversByRecord({
service,
record_data,
form_name
});
}
/*get fields*/
export const loadFormFields=(formName,serverName)=>{
return post('/hr/loadFormFields',{
formName:formName,
baseUrl:serverName
});
}
/*init field validate*/
const createValidateObject=(key,fieldType,validates,validateObject)=>{
validateObject[key]={
'required':validates.is_required?validates.is_required:false,
'max':validates.max_length?validates.max_length:9999,
'min':validates.min_length?validates.min_length:0,
'trim':/(^\s*)|(\s*$)/g
};
if('mobile'==fieldType)
validateObject[key]['mobile']=/^(17[0-9]|13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i;
if('email'==fieldType){
validateObject[key]['email']=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
}
};
const createTabularValidateObject=(key,sectionType,fields,validateTabularObject)=>{
validateTabularObject[key]={};
fields.map((field,i)=>{
if(field.options&&!field.is_invisible_create){
const options=JSON.parse(field.options);
if(field.type=='lookup')
createValidateObject(field.name+'_ID',field.type,options,validateTabularObject[key]);
else
createValidateObject(field.name,field.type,options,validateTabularObject[key]);
}
});
};
const initValidateObject=(validateObject,validateTabularObject,formDefine)=>{
formDefine.map((section,index)=>{
if(section.type=='tabular'&§ion.fields&§ion.fields.length>0){
createTabularValidateObject(section.name,section.type,section.fields,validateTabularObject);
}else if(section.fields&§ion.fields.length>0){
section.fields.map((field,i)=>{
if(field.options&&!field.is_invisible_create){
const options=JSON.parse(field.options);
if(field.type=='lookup')
createValidateObject(field.name+'_ID',field.type,options,validateObject);
else
createValidateObject(field.name,field.type,options,validateObject);
}
});
}
});
}
const valiField=(fieldVali,fieldValue)=>{
if(fieldVali.required&&!fieldValue){
return '此项是必填项';
}else if(fieldValue){
if(fieldVali.max){
if(fieldValue.length<fieldVali.min||fieldValue.length>fieldVali.max)
return `当前项输入字符数只能在${fieldVali.min}-${fieldVali.max+1}之间`;
}
if(fieldVali.mobile){
if(!fieldVali.mobile.test(fieldValue))
return '请输入正确的手机号码';
}
if(fieldVali.email){
if(!fieldVali.email.test(fieldValue))
return '请输入正确的电子邮箱地址';
}
if(fieldValue.replace&&!fieldValue.replace(fieldVali.trim,"")){
return '此项不能全是空格';
}
}
}
const getValueErrors=(validateObject,values,errors)=>{
for(let fieldKey in validateObject){
errors[fieldKey]=valiField(validateObject[fieldKey],values[fieldKey]);
}
};
const getTabularValueErrors=(validateTabularObject,values,errors)=>{
Object.keys(validateTabularObject).map((key,keyIndex)=>{
errors[key]=[];
values[key].map((item,index)=>{
let tempField={};
for (let fieldTabularKey in validateTabularObject[key]){
tempField[fieldTabularKey]=valiField(validateTabularObject[key][fieldTabularKey],item[fieldTabularKey]);
}
errors[key].push(tempField);
});
})
};
export const getValidate=(formDefine)=>{
let validateObject={};
let validateTabularObject={};
if(formDefine&&formDefine.length>0){
initValidateObject(validateObject,validateTabularObject,formDefine);
}
return (values)=>{
const errors = {};
getValueErrors(validateObject,values,errors);
getTabularValueErrors(validateTabularObject,values,errors);
return errors;
}
}
/*init field keys*/
const pushTabularFieldKey=(field,section,fieldKeys)=>{
if(field.type=='lookup'&&field.name){
fieldKeys.push(section.name+'[].'+field.name+'_ID');//related_form_name
}else if(field.name)
fieldKeys.push(section.name+'[].'+field.name);
};
const pushFieldKey=(field,fieldKeys)=>{
if(field.type=='lookup'&&field.name){
fieldKeys.push(field.name+'_ID');//related_form_name
}else if(field.name)
fieldKeys.push(field.name);
};
export const getFieldKeys=(formDefine)=>{
const fieldKeys=[];
if(formDefine&&formDefine.length>0){
formDefine.map((section,index)=>{
if(section.type=='tabular'&§ion.fields&§ion.fields.length>0){
section.fields.map((field,i)=>{
if(field&&!field.is_invisible_create)
pushTabularFieldKey(field,section,fieldKeys);
});
}else if(section.fields&§ion.fields.length>0){
section.fields.map((field,i)=>{
if(!field.is_invisible_create)
pushFieldKey(field,fieldKeys);
});
}
})
}
return fieldKeys;
}
/*handle initial values*/
export const handleInitialValues=(values)=>{
if(values&&values.tabular_sections){
const temp=Object.assign({}, values);
delete temp.tabular_sections;
return Object.assign({}, values.tabular_sections,temp);
}else
return values;
}
/*format tabular values*/
export const formatTabularValues=(values,formDefine)=>{
const tempValues=Object.assign({}, values);
const tabular_sections={};
let hasTabular=false;
formDefine.map((section,index)=>{
if(section.type=='tabular'){
tabular_sections[section.name]=values[section.name];
delete tempValues[section.name];
hasTabular=true;
}
})
if(hasTabular)
tempValues['tabular_sections']=tabular_sections;
return tempValues;
}
/*handle ajax back error mesage*/
export const getErrors=(res,reject)=>{
if(res&&res.code&&res.code>=300&&res.code!=400){
reject({'_error':res.message})
return true;
}else if(res&&res.code&&res.code==400){
const errors=_handleError(res);
console.log(errors);
reject(errors)
return true;
}else if(res&&res.code&&res.code<300){
return false;
}else{
return false;
}
}
const _handleError=(res)=>{
if(res&&res.errors){
const keys=Object.keys(res.errors);
const backErro={'_error':res.message};
keys.map(key=>{
backErro[key]=res.errors[key].join(",");
});
return backErro;
}else{
return {'_error':res.message};
}
}
export const getReduxFormFieldKeys=(formDefine)=>{
const {basicInfoSection=[],nonTabularSection=[],fileUploadSection=[],tabularSection=[]}=formDefine;
const keys=[];
basicInfoSection.map(field=>{
const {type,name}=field;
if(field.type=='lookup'&&name){
keys.push(name+'_ID');//related_form_name
}else if(name)
keys.push(name);
});
nonTabularSection.map(field=>{
const {type,name}=field;
if(field.type=='lookup'&&name){
keys.push(name+'_ID');//related_form_name
}else if(name)
keys.push(name);
});
fileUploadSection.map(fileField=>{
const {name}=fileField;
if(name)
keys.push(name);
});
tabularSection.map(tabular=>{
const {type,name,fields}=tabular;
fields.map(field=>{
keys.push('tabular_sections.'+name+'[].'+field.name);
});
});
return keys;
}
const initReduxFormValidateObject=(validateObject,validateTabularObject,formDefine)=>{
const {basicInfoSection=[],nonTabularSection=[],fileUploadSection=[],tabularSection=[]}=formDefine;
basicInfoSection.map((field,i)=>{
if(field.options&&!field.is_invisible_create){
const options=JSON.parse(field.options);
if(field.type=='lookup')
createValidateObject(field.name+'_ID',field.type,options,validateObject);
else
createValidateObject(field.name,field.type,options,validateObject);
}
});
nonTabularSection.map((field,i)=>{
if(field.options&&!field.is_invisible_create){
const options=JSON.parse(field.options);
if(field.type=='lookup')
createValidateObject(field.name+'_ID',field.type,options,validateObject);
else
createValidateObject(field.name,field.type,options,validateObject);
}
});
fileUploadSection.map((field,i)=>{
if(field.options&&!field.is_invisible_create){
const options=JSON.parse(field.options);
createValidateObject(field.name,field.type,options,validateObject);
}
});
tabularSection.map((section,i)=>{
createTabularValidateObject(section.name,section.type,section.fields,validateTabularObject);
});
}
export const getReduxFormValidate=(formDefine)=>{
let validateObject={};
let validateTabularObject={};
initReduxFormValidateObject(validateObject,validateTabularObject,formDefine);
return (values)=>{
const errors = {};
getValueErrors(validateObject,values,errors);
getTabularValueErrors(validateTabularObject,values,errors);
return errors;
}
}
export default {
getFieldKeys,
getValidate,
handleInitialValues,
formatTabularValues
};