@@ -67,7 +67,6 @@ const defaultProps: IFormProps = {
6767 prefixCls : "nex-form" ,
6868 className : "" ,
6969 style : { } ,
70- //实验性质,有序可能移除
7170 disableValidator : false ,
7271 validators : { } ,
7372 path2obj : true ,
@@ -106,7 +105,7 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
106105 fields : FormItem [ ] = [ ] ;
107106 _validateCb : ValueChangeCallback [ ] = [ ] ;
108107
109- state = {
108+ state : IFormState = {
110109 formError : { } ,
111110 validatingFields : { } ,
112111 formValue : this . props . defaultFormValue || { } ,
@@ -130,6 +129,17 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
130129 }
131130 }
132131
132+ getFieldByName ( name : string ) {
133+ const fields = this . fields ;
134+ for ( let i = 0 ; i < fields . length ; i ++ ) {
135+ if ( name === fields [ i ] . props . name ) {
136+ return fields [ i ] ;
137+ }
138+ }
139+
140+ return null ;
141+ }
142+
133143 getInitialFormValue ( ) {
134144 const initialFormValue : FormValue = { } ;
135145
@@ -296,17 +306,48 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
296306 this . _validateCb = [ ] ;
297307 }
298308
309+ isDisableValidator ( name ?: string ) {
310+ if ( arguments . length ) {
311+ return this . isDisableValidatorField ( name as string ) ;
312+ }
313+
314+ return this . props . disableValidator ;
315+ }
316+
317+ isDisableValidatorField ( name : string ) {
318+ const field = this . getFieldByName ( name ) ;
319+ if ( ! field ) return true ;
320+
321+ return field . getProp ( "disableValidator" , false ) ;
322+ }
323+
299324 hasError ( name : string ) {
325+ if ( this . isDisableValidatorField ( name ) ) {
326+ return false ;
327+ }
328+
300329 const { formError } = this . state ;
301330
302331 return formError [ name ] != null ; // null or undefined
303332 }
304333
305334 getError ( name : string ) {
335+ if ( this . isDisableValidatorField ( name ) ) {
336+ return null ;
337+ }
338+
306339 const { formError } = this . state ;
307340 return formError [ name ] ;
308341 }
309342
343+ getErrors ( ) {
344+ if ( this . isDisableValidator ( ) ) {
345+ return { } ;
346+ }
347+
348+ return this . state . formError ;
349+ }
350+
310351 cleanError ( name : string ) {
311352 const { formError } = this . state ;
312353
@@ -323,6 +364,10 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
323364 }
324365
325366 setError ( name : string , message : any ) {
367+ if ( this . isDisableValidatorField ( name ) ) {
368+ return ;
369+ }
370+
326371 const { formError } = this . state ;
327372 this . setState ( {
328373 formError : {
@@ -341,8 +386,10 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
341386 setErrors ( errors : Record < string , any > ) {
342387 const { formError } = this . state ;
343388 this . setState ( {
344- ...formError ,
345- ...errors ,
389+ formError : {
390+ ...formError ,
391+ ...errors ,
392+ } ,
346393 } ) ;
347394 }
348395
@@ -390,11 +437,16 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
390437 }
391438
392439 isFieldValidating ( name : string ) {
440+ if ( this . isDisableValidatorField ( name ) ) {
441+ return false ;
442+ }
443+
393444 const validatingFields = this . state . validatingFields ;
394445 return ! ! validatingFields [ name ] ;
395446 }
396447
397448 isValidating ( ) {
449+ if ( this . props . disableValidator ) return false ;
398450 if ( this . _isFormValidating ) return true ;
399451
400452 const validatingFields = this . state . validatingFields ;
@@ -490,7 +542,12 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
490542 let asyncTimer : number | null = ( setTimeout ( ( ) => {
491543 asyncTimer = null ;
492544
493- if ( lockId !== this . fieldLocks [ name ] ) return ;
545+ if (
546+ lockId !== this . fieldLocks [ name ] ||
547+ this . isDisableValidatorField ( name )
548+ ) {
549+ return ;
550+ }
494551
495552 this . setState ( {
496553 validatingFields : {
@@ -511,7 +568,10 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
511568 clearTimeout ( asyncTimer ) ;
512569 }
513570
514- if ( lockId !== this . fieldLocks [ name ] ) {
571+ if (
572+ lockId !== this . fieldLocks [ name ] ||
573+ this . isDisableValidatorField ( name )
574+ ) {
515575 callback ( errors , value , true /* abort state */ ) ;
516576 return ;
517577 }
@@ -553,7 +613,7 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
553613 this . _isFormValidating = true ;
554614
555615 const updateFormState = ( ) => {
556- if ( lockId !== this . formLockId ) return ;
616+ if ( lockId !== this . formLockId || this . isDisableValidator ( ) ) return ;
557617
558618 this . setState ( {
559619 formError,
@@ -564,7 +624,7 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
564624 const complete = ( errors : ValidationError [ ] | null , name : string ) => {
565625 validCounter -- ;
566626
567- if ( errors ) {
627+ if ( errors && ! this . isDisableValidatorField ( name ) ) {
568628 formError [ name ] = errors [ 0 ] . message ;
569629 allErrors . push ( ...errors ) ;
570630 }
@@ -576,7 +636,7 @@ export class Form extends React.Component<Partial<IFormProps>, IFormState> {
576636 asyncUpdateTimer = null ;
577637 }
578638
579- if ( lockId !== this . formLockId ) {
639+ if ( lockId !== this . formLockId || this . isDisableValidator ( ) ) {
580640 callback (
581641 allErrors . length ? allErrors : null ,
582642 formValue ,
0 commit comments