ng-bootstrap-form-validation icon indicating copy to clipboard operation
ng-bootstrap-form-validation copied to clipboard

Custom error on form control with custom validator (AbstractControl, ValidatorFn)

Open ruslanpascoal2 opened this issue 5 years ago • 0 comments

I have a custom validator (cpfCnpjValidator) passed to the form control like so:

this.form = this.formBuilder.group({
      document: ["", [Validators.required, cpfCnpjValidator()]]
});

How should it be referenced in the ErrorMessage array?

customErrorMessages: ErrorMessage[] = [
    {
      error: "pattern",
      format: (label, error) => `${label.toUpperCase()} inválido.`,
    },
  ];
  • error key expects a string, passing it an empty string ("") doesn't work as well.

template:

 <div class="form-group" [customErrorMessages]="customErrorMessages">
            <label for="cpfCnpjInput">CPF / CNPJ</label>
            <input
              type="text"
              class="form-control"
              id="cpfCnpjInput"
              autocomplete="off"
              mask="000.000.000-00||00.000.000/0000-00"
              formControlName="document"
            />
</div>

ruslanpascoal2 avatar Dec 28 '20 13:12 ruslanpascoal2