0

I have using reactive form and form have 60 fields. so I have written asyncvalidtors for server-side validation.

Note: Not using value changes mechanism.it works with below two line

   formGroup.ProductId.setErrors(null, { onlySelf: true, emitEvent: false });
     formGroup.ProductId.updateValueAndValidity({ onlySelf: true, emitEvent:false}); 

Problem with Validators Mechanism

I have faced an issue in the following scenarios. In async validators, I have use case if productName control is valid and set error null for productId fields.i have set the errors, and while perform updateValueandValidity send another request to the server .

if(result.controlName =='ProductName' && result.Validation==true){

// if control Name is productName // set Errors as Null for  Product ID

formGroup.ProductId.setErrors(null, { onlySelf: true, emitEvent: false });
 formGroup.ProductId.updateValueAndValidity({ onlySelf: true, emitEvent: false });
//But it recursively call  while perform this line


}

validators

 export class SomeAsyncValidator {
       static createValidator = (someService: SomeService) => (control: AbstractControl) =>
           timer(500)
               .pipe(
debounceTime(2000)
                // If previous query is different from current
                , distinctUntilChanged(),
                   map(() => control.value),
                   switchMap((name) => someService.exists({ request })),
                   map(() => ({
    
    if(result.controlName =='ProductName' && result.Validation==true){
    
    // if control Name is productName // set Errors as Null for  Product ID
    
    formGroup.ProductId.setErrors(null, { onlySelf: true, emitEvent: false });
     formGroup.ProductId.updateValueAndValidity({ onlySelf: true, emitEvent: false });
    //But it recursively call  while perform this line
    
    
    }
    
    })),
                   catchError(() => of(null)));
    }
7
  • 1
    maybe you can try to call updateValueAndValidity({ onlySelf: true }) Commented Sep 14, 2020 at 13:39
  • 1
    sorry tried { onlySelf: true, emitEvent: false }forget to add in question updated Commented Sep 14, 2020 at 13:42
  • it works for value changes not for validators Commented Sep 14, 2020 at 13:44
  • okay then maybe try to add distinctUntilChanged() in the pipe Commented Sep 14, 2020 at 13:45
  • 1
    gona say your error is caused by calling setErrors manually while trying to use valdiators. you shouldn't be doing that, so this seems like a design problem. if your validation involves multiple fields, it should probably be a group validator. you should describe the actual problem you're trying to solve here so you can get help with your design. Commented Sep 14, 2020 at 13:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.