Skip to content

Commit bb84744

Browse files
committed
Modified src/Form.js
Modified src/FormItem.js
1 parent 0a8168c commit bb84744

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

src/Form.js

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,23 @@ export default class Form extends React.Component {
168168
}
169169

170170
if (fieldProps.validator) {
171-
fieldValidators.push(fieldProps.validator);
171+
fieldValidators.push(
172+
...(Array.isArray(fieldProps.validator)
173+
? fieldProps.validator
174+
: [fieldProps.validator])
175+
);
172176
}
173177
});
174178

175179
const validator = this.props.validators[name];
176180

177181
if (validator) {
178-
fieldValidators.push(validator);
182+
fieldValidators.push(
183+
...(Array.isArray(validator) ? validator : [validator])
184+
);
179185
}
180186

181-
return fieldValidators;
187+
return fieldValidators.filter(v => typeof v === "function");
182188
}
183189

184190
getFieldRules(name) {
@@ -227,30 +233,36 @@ export default class Form extends React.Component {
227233
return !!validatingFields[name];
228234
}
229235

230-
validateField(name, cb) {
236+
validateField(name, callback) {
231237
const { formError, validatingFields } = this.state;
232238
const value = this.getValue(name);
233-
const rules = this.getFieldRules(name);
239+
const validators = this.getFieldValidator(name);
234240

235-
if (!rules || rules.length === 0) {
236-
if (cb instanceof Function) {
237-
cb(null, value);
241+
const cb = (errors = null) => {
242+
if (errors === null && validators.length) {
243+
startCheck();
244+
return;
238245
}
239-
return;
240-
}
241246

242-
this.setState({
243-
validatingFields: {
244-
...validatingFields,
245-
[name]: true
246-
}
247-
});
247+
if (errors !== null) {
248+
if (!Array.isArray(errors)) {
249+
errors = [errors];
250+
}
248251

249-
const descriptor = { [name]: rules };
250-
const validator = new AsyncValidator(descriptor);
251-
const data = { [name]: value };
252+
errors = errors.map(error => {
253+
let message = error;
254+
255+
if (error instanceof Error) {
256+
message = error.message;
257+
}
258+
259+
return {
260+
name,
261+
message
262+
};
263+
});
264+
}
252265

253-
validator.validate(data, { firstFields: true }, errors => {
254266
this.setState(
255267
{
256268
formError: {
@@ -263,12 +275,81 @@ export default class Form extends React.Component {
263275
}
264276
},
265277
() => {
266-
if (cb instanceof Function) {
267-
cb(errors, value);
278+
if (typeof callback === "function") {
279+
callback(errors, value);
268280
}
269281
}
270282
);
283+
};
284+
285+
const startCheck = () => {
286+
const validator = validators.shift();
287+
288+
if (!validator) {
289+
return; //check finish
290+
}
291+
292+
const ret = validator(value);
293+
if (ret === true) {
294+
cb();
295+
} else if (ret === false) {
296+
cb(`${name} fails`);
297+
} else if (ret && ret.then) {
298+
//thenable
299+
ret.then(() => cb(), e => cb(e));
300+
} else {
301+
cb(ret);
302+
}
303+
};
304+
305+
this.setState({
306+
validatingFields: {
307+
...validatingFields,
308+
[name]: true
309+
}
271310
});
311+
312+
startCheck();
313+
314+
// const rules = this.getFieldRules(name);
315+
316+
// if (!rules || rules.length === 0) {
317+
// if (cb instanceof Function) {
318+
// cb(null, value);
319+
// }
320+
// return;
321+
// }
322+
323+
// this.setState({
324+
// validatingFields: {
325+
// ...validatingFields,
326+
// [name]: true
327+
// }
328+
// });
329+
330+
// const descriptor = { [name]: rules };
331+
// const validator = new AsyncValidator(descriptor);
332+
// const data = { [name]: value };
333+
334+
// validator.validate(data, { firstFields: true }, errors => {
335+
// this.setState(
336+
// {
337+
// formError: {
338+
// ...formError,
339+
// [name]: errors ? errors[0].message : null
340+
// },
341+
// validatingFields: {
342+
// ...validatingFields,
343+
// [name]: false
344+
// }
345+
// },
346+
// () => {
347+
// if (cb instanceof Function) {
348+
// cb(errors, value);
349+
// }
350+
// }
351+
// );
352+
// });
272353
}
273354

274355
validate(callback) {

src/FormItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class FormItem extends React.Component {
2020
PropTypes.array,
2121
PropTypes.func
2222
]),
23-
validator: PropTypes.func,
23+
validator: PropTypes.oneOfType([PropTypes.func, PropTypes.array]),
2424
// asyncValidator: PropTypes.func,
2525
required: PropTypes.bool,
2626
normalize: PropTypes.func,

0 commit comments

Comments
 (0)