1

To set the validator inside the form group in the form arrayI tried this method described in this link:

Not Able To Set Dynamically Validator Inside FormGroup in FormArray - Angular

I tried this, and it worked perfectly for adding validators, but it doesn't work for deleting them.

I tried the remove validators method and the clear validators method, but they don't work.

// let groupItems:any = (this.formGroup.get("address") as FormArray).controls;

const groupItems: any = (address as FormArray).controls;

  for (const item of groupItems) {
    item.controls.city.clearValidators();
    item.controls.state.clearValidators();
  }

What is the approach to delete a dynamic validator to formArray inside formGroup?

Update

I tried to add `updateValueAndValidity(), but it gave me a stack error:

    for (const item of groupItems) 
        item.controls.city.clearValidators();
        item.controls.city.updateValueAndValidity();
        item.controls.state.clearValidators();
        item.controls.state.updateValueAndValidity();
      }

   SO:
to avoid LoggingService: Maximum call stack size exceeded while using I add a listener to the selector that triggers validation changes with the take(1) rxJS pipe and set validations like this:

          Selector?.valueChanges
      .pipe(take(1))
      .subscribe(SelectorRes => {
        if (SelectorRes === true) {
          const groupItems: any = (address as FormArray)
            .controls;

          for (const item of groupItems) {
            item.controls.city.setValidators(
              Validators.required
            );
            item.controls.state.setValidators(Validators.required);
          }

          //   !was
          console.log('call validation 1');
        } else if (
          SelectorRes === false
        ) {
          const groupItems: any = (address as FormArray)
            .controls;

          for (const item of groupItems) {
            item.controls.city.clearValidators();
            item.controls.city.updateValueAndValidity()
            item.controls.state.clearValidators();
            item.controls.state.updateValueAndValidity();
          }

          //   !was
          console.log('delete validation 2');
        }
      });

It works, but it calls too many times in one change, I think:

  • (25) call validation 1 -- foo.validator.ts
  • (144) delete validation 2 -- foo.validator.ts
6
  • A question, why do you want to remove the validators? Generally it's better use a custom conditional field validator Commented Mar 9, 2023 at 10:36
  • I don't understand the question: What do you mean by "conditional field validator" ? Commented Mar 9, 2023 at 11:26
  • a Custom form control that depends from another variable or another formControl (e.g. the "typical" repeat password or a "want to received by email" that make mandatory the email) Commented Mar 9, 2023 at 14:02
  • can you give me an example ? Commented Mar 9, 2023 at 14:04
  • 1
    The last link I see is here, but in SO there're multiple examples, e.g. this SO Commented Mar 9, 2023 at 19:56

1 Answer 1

0

According to Angular documentation:

When you add or remove a validator at run time, you must call updateValueAndValidity() for the new validation to take effect.

You can do something like this:

const groupItems: any = (address as FormArray).controls;

  for (const item of groupItems) {
    item.controls.city.clearValidators();
    item.controls.city.updateValueAndValidity();
    item.controls.state.clearValidators();
    item.controls.state.updateValueAndValidity();
  }
Sign up to request clarification or add additional context in comments.

2 Comments

when I use item.controls.city.updateValueAndValidity(); It works but gives me an error: LoggingService: Maximum call stack size exceeded I tried to use it: updateValueAndValidity() onlySelf: true, emitEvent: false }); but it doesn't work.
I think that I need parent (main form Group) to be validated too, but I can't think of doing it without onlySelf: false

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.