Musheer Aiman
Musheer Aiman

Reputation: 175

Disable button while ngForm is invalid

I have an add modal in that I need the SAVE button to be disabled till the contents are filled. As the contents are filled the button should get enabled.Here is my code

<div bsModal #editModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md">
    <div class="modal-content">
        <form name='groupForm' #groupForm="ngForm" novalidate>
            <div class="modal-header">
                <h4 class="modal-title wd-title-popup">Add Access Group</h4>
            </div>
            <div class="modal-body row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 accssContent paddingLeftClass">
                    <md-input-container style='width: 100%;margin-bottom: 6px !important;'>
                        <input type="textbox" mdInput name="admin" [(ngModel)]="group.groupName" placeholder="Access Group Name" required>
                    </md-input-container>
                </div>
            </div>
            <div md-theme="reports" class="modal-footer" style="text-align: center;">
                <div layout-align="end center" layout="row">
                    <button md-raised-button class="md-raised color-white" (click)="editModal.hide()" style="width: 45%;margin: 10px 5px;background-color: #FF5252;">Cancel</button>
                    <button md-raised-button class="md-raised color-white" [disabled]="!groupForm.form.valid" (click)="accessGroup(group)" style="width: 45%;margin: 10px 5px;background-color:#58B6A2;">Save</button>
                </div>
            </div>
        </form>
    </div>
</div>

This same is working fine with other buttons. Whats am I doing wrong here?

Upvotes: 1

Views: 2090

Answers (2)

FAISAL
FAISAL

Reputation: 34683

Try using the invalid property. Change it like this:

<button md-raised-button class="md-raised color-white"  
        (click)="accessGroup(group)" 
        [disabled]="groupForm.invalid">Save</button>

Link to Plunker Demo.

Upvotes: 2

Carsten
Carsten

Reputation: 4208

Your form has novalidate which might cause the form to skip validation and not set the valid variable

Upvotes: 0

Related Questions