Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show errors from different controls conditionally #28

Open
dev054 opened this issue Jan 20, 2021 · 1 comment
Open

Show errors from different controls conditionally #28

dev054 opened this issue Jan 20, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@dev054
Copy link

dev054 commented Jan 20, 2021

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[x] Support request
[ ] Other... Please describe:

Problem

I have this component:

function invalidTouched(control: AbstractControl | null | undefined): boolean {
  return !!(control?.invalid && control.touched);
}

export class CustomErrorStateMatcher implements ErrorStateMatcher {
  isErrorState(control: FormControl | null): boolean {
    return invalidTouched(control) || invalidTouched(control?.parent);
  }
}

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'app-test',
  template: `
    <form errorTailor [formGroup]="formGroup">
      <!-- password field omitted for brevity --> 
      <mat-form-field>
        <mat-label>Password confirmation</mat-label>
        <input
          matInput
          required
          formControlName="passwordConfirmation"
          type="password"
          [controlErrorAnchor]="passwordConfirmationControlErrorAnchor"
          [errorStateMatcher]="customErrorStateMatcher"
        />
        <mat-error>
          <ng-template
            controlErrorAnchor
            #passwordConfirmationControlErrorAnchor="controlErrorAnchor"
          ></ng-template>
          <!-- Here I want to show passwordConfirmation OR formGroup errors, if passwordConfirmation has no errors. -->
        </mat-error>
      </mat-form-field>
    </form>
  `,
})
class TestComponent {
  readonly customErrorStateMatcher = new CustomErrorStateMatcher();
  readonly formGroup = this.formBuilder.group(
    {
      password: ['', [Validators.required]],
      passwordConfirmation: ['', [Validators.required]],
    },
    { validators: this.equalToValidator },
  );

  equalToValidator(group: FormGroup) {
    const password = group.get('password').value;
    const passwordConfirmation = group.get('passwordConfirmation').value;

    return password === passwordConfirmation ? null : { equalTo: true }     
  }
}

... and the problem is that I need to show the equalTo error on the passwordConfirmationControlErrorAnchor template, but only if passwordConfirmation control itself has no error.

Is that possible?

Environment


Angular version: 11.0.0
@NetanelBasal NetanelBasal added the enhancement New feature or request label Jan 21, 2021
@rohitattaluri552
Copy link

@NetanelBasal do the errorTailor works for html select textarea and radio-buttons tag as well ?

If yes, please let me know how to show the error-messages for the above types. (Btw I'm working on Angular 11, and Material modules for building reactive forms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants