1

I am building a generic form using Angular reactive forms. I have the following generic Html for a input elements

<div class="form-input form-group" [formGroup]="group">
    <div class="row">
        <div class="col-2 font-label">
            <label>{{ config.label }}</label>
        </div>
        <div class="col-10">
            <input type="text" [attr.placeholder]="config.placeholder" disabled="disabled" class="form-control"
                [formControlName]="config.name">
            <div [hidden]="!(group.controls[config.name].invalid && group.controls[config.name].touched)">
                <small class="form-text text-danger" [hidden]="!group.controls[config.name]?.errors?.required"></small>
                <small class="form-text text-danger" [hidden]="!group.controls[config.name]?.errors?.minlength"></small>
                <small class="form-text text-danger" [hidden]="!group.controls[config.name]?.errors?.maxlength"></small>
            </div>
        </div>
    </div>
</div>

But for the following !group.controls[config.name]?.errors?.required its telling me Identifier 'required' is not defined . Same for minlength and maxlength. From where i can get minlength and maxlength and required errors?

1
  • 1
    Can you provide a plunker? Commented Jun 23, 2017 at 9:36

2 Answers 2

1

try this

<small class="form-text text-danger" [hidden]="!group.controls[config.name].hasError('required')"></small>
<small class="form-text text-danger" [hidden]="!group.controls[config.name].hasError('minlength')"></small>
<small class="form-text text-danger" [hidden]="!group.controls[config.name].hasError('maxlength')"></small>
Sign up to request clarification or add additional context in comments.

Comments

0

You can take advantage of the second parameter of AbstractControl#hasError method using it like this:

<small class="form-text text-danger" 
       [hidden]="!group.hasError('required', config.name)">
</small>

Also, I'd recommend you to use *ngIf instead of hidden.

You can read this for a deep explanation.

Comments

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.