简体   繁体   中英

I want to validate that input-label has a value if other input-control has set certain value

As I said in the title, I have a dropdown list with 2 values - yes and no, and another input label where you can write freely. I want to validate that if the user chose "yes", also the input label is filled and vice versa - if "no" was chosen I want to make sure that the input label is empty.

For making sure, that if "no" was chosen label will be empty, I tried simply blocking the label when "no" is chosen but it is not enough as a user can choose "yes" then write something and switch back to "no"

<div class="select-wrapper" [ngClass]="{'select-wrapper-blocked': isNotAdmin()}">
            <select class="input-control" [(ngModel)]="booleanVariable">
                <option value="false">No</option>
                <option value="true">Yes</option>
            </select>
        </div>
    </div>

    <div class="col form-input" [ngClass]="{'form-input-blocked': isNotAdmin()}">
        <p class="input-label">
            Some text
        </p>
        <input class="input-control" [(ngModel)]="stringVariable" />
    </div>

You can wrap input label in a <span> and display it only when user selects "yes" ie booleanVariable is true

Try like this:

<p class="input-label">
    <span *ngIf="booleanVariable">  Some text </span>
</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM