@@ -233,10 +233,15 @@ export default class Form extends React.Component {
233233 return ! ! validatingFields [ name ] ;
234234 }
235235
236- _validateField ( name , callback ) {
236+ _validateField ( name , callback = ( ) => { } ) {
237237 const value = this . getValue ( name ) ;
238238 const validators = this . getFieldValidator ( name ) ;
239239
240+ if ( ! validators . length ) {
241+ callback ( ) ;
242+ return ;
243+ }
244+
240245 const cb = ( errors = null ) => {
241246 if ( errors === null && validators . length ) {
242247 startCheck ( ) ;
@@ -262,9 +267,7 @@ export default class Form extends React.Component {
262267 } ) ;
263268 }
264269
265- if ( typeof callback === "function" ) {
266- callback ( errors , value ) ;
267- }
270+ callback ( errors , value ) ;
268271 } ;
269272
270273 const startCheck = ( ) => {
@@ -293,10 +296,23 @@ export default class Form extends React.Component {
293296 validateField ( name , callback ) {
294297 const { formError, validatingFields } = this . state ;
295298 //是否异步探测
296- let isAsync = true ;
299+ const asyncMaxTime = 100 ; //校验时间小于100ms不算异步
300+ let asyncTimer = setTimeout ( ( ) => {
301+ asyncTimer = null ;
302+ this . setState ( {
303+ validatingFields : {
304+ ...validatingFields ,
305+ [ name ] : true
306+ }
307+ } ) ;
308+ } , asyncMaxTime ) ;
309+ // let isAsync = true;
297310
298311 this . _validateField ( name , ( errors , value ) => {
299- isAsync = false ;
312+ if ( asyncTimer ) {
313+ clearTimeout ( asyncTimer ) ;
314+ }
315+ // isAsync = false;
300316
301317 this . setState (
302318 {
@@ -316,68 +332,71 @@ export default class Form extends React.Component {
316332 }
317333 ) ;
318334 } ) ;
319-
320- if ( isAsync ) {
321- this . setState ( {
322- validatingFields : {
323- ...validatingFields ,
324- [ name ] : true
325- }
326- } ) ;
327- }
328335 }
329336
330- validate ( callback ) {
337+ validate ( callback = ( ) => { } ) {
331338 const { formValue } = this . state ;
332339 const fields = this . fields ;
333- const rules = { } ;
340+ const validCounter = 0 ;
341+ const asyncMaxTime = 100 ; //校验时间小于100ms不算异步
342+ const validatingFields = { } ;
343+ const formError = [ ] ;
344+ let asyncTimer = setTimeout ( ( ) => {
345+ asyncTimer = null ;
346+ this . setState ( {
347+ validatingFields
348+ } ) ;
349+ } , asyncMaxTime ) ;
334350
335- if ( fields . length === 0 && callback ) {
336- callback ( null , formValue ) ;
337- return ;
338- }
351+ const complete = ( errors , name ) => {
352+ validCounter -- ;
339353
340- fields . forEach ( field => {
341- const name = field . props . name ;
342- if ( ! name || name in rules ) return ;
343- const fieldRules = this . getFieldRules ( name ) ;
344- if ( fieldRules && fieldRules . length ) {
345- rules [ name ] = fieldRules ;
354+ if ( errors ) {
355+ // formError[name] = errors[0].message;
356+ formError . push ( ...errors ) ;
346357 }
347- } ) ;
358+ if ( validCounter <= 0 ) {
359+ this . setState (
360+ {
361+ formError,
362+ validatingFields : { }
363+ } ,
364+ ( ) => {
365+ callback (
366+ formError . length ? formError : null ,
367+ formValue
368+ ) ;
369+ }
370+ ) ;
371+ }
372+ } ;
348373
349- const validatingFields = { } ;
374+ if ( fields . length ) {
375+ //校验初始化
376+ fields . forEach ( field => {
377+ const name = field . props . name ;
378+ validCounter ++ ;
379+ validatingFields [ name ] = true ;
380+ formError [ name ] = null ;
381+ } ) ;
350382
351- const validator = new AsyncValidator ( rules ) ;
352- const data = { } ;
353- Object . keys ( rules ) . forEach ( key => {
354- data [ key ] = get ( formValue , key ) ;
355- validatingFields [ key ] = true ;
356- } ) ;
383+ //开始进行字段校验
384+ fields . forEach ( field => {
385+ const name = field . props . name ;
357386
358- this . setState ( {
359- validatingFields
360- } ) ;
387+ this . _validateField ( name , errors => {
388+ validatingFields [ name ] = false ;
389+ if ( asyncTimer ) {
390+ clearTimeout ( asyncTimer ) ;
391+ asyncTimer = null ;
392+ }
361393
362- validator . validate ( data , { firstFields : true } , errors => {
363- const formError = { } ;
364- if ( errors ) {
365- errors . forEach ( error => {
366- formError [ error . field ] = error . message ;
394+ complete ( errors , name ) ;
367395 } ) ;
368- }
369- this . setState (
370- {
371- formError,
372- validatingFields : { }
373- } ,
374- ( ) => {
375- if ( callback instanceof Function ) {
376- callback ( errors , formValue ) ;
377- }
378- }
379- ) ;
380- } ) ;
396+ } ) ;
397+ } else {
398+ callback ( null , formValue ) ;
399+ }
381400 }
382401
383402 validateAndScroll ( callback ) {
0 commit comments