Victor Soares
Victor Soares

Reputation: 880

How to conditionally require form inputs in Ionic?

I´m trying to put the required property in a input tag, but it's not working like in Angular 2+:

HTML:

<ion-input name="firstName"
           [(ngModel)]="firstName"  
           [attr.required]="checkRequired()" >
</ion-input>

<span *ngIf="formulario.controls.firstName?.invalid"> Required </span>

component.ts:

checkRequired() {
    return true;
}

This code should show a span "Required" bellow the input when it's not filled. But it does not.

I already tryed this variations:

[attr.required]="true"
[attr.required]="checkRequired() ? 'true' : null"
[attr.required]="checkRequired() ? 'true' : false"
[attr.required]="checkRequired() ? 'required' : null"
[attr.required]="checkRequired() ? 'required' : false"

Upvotes: 0

Views: 793

Answers (1)

Victor Soares
Victor Soares

Reputation: 880

I solve the problem like this:

[required]="checkRequired()"

Upvotes: 3

Related Questions