1

I have a submit button in my form, i am trying to disable it until all the text-boxes are filled. Its not working after page is refreshed or loaded for the first time in browser. If i perform any other operation in the same page and then open this add detail pop up it is disabled. Help will be much appreciated.

component

form: FormGroup;
 constructor(private fb: FormBuilder) {
    this.createForm();
}
createForm() {
    this.form = this.fb.group({
        name: ['', Validators.required],
        addrs: ['', Validators.required],
    });
}

HTML file

<div id="details">
    <form [formGroup]="form" (ngSubmit)="addDetails()" name="form" novalidate>
        <div class="form-group">
            <label for="name">Name</label><input type="text"
                class="form-control" id="name" placeholder="Please Enter Name"
                formControlName="name" >
        </div>
        <div class="form-group">
            <label for="addrs">Address</label><input type="text"
                class="form-control" id="addrs"
                placeholder="Please Enter Address" formControlName="addrs" >

        <button type="submit" [disabled]="form.invalid || loading"
            class="btn btn-success">Submit</button>
        <br>
        <div *ngIf="loading" class="alert alert-success box-msg" role="alert">
            <strong>Added Successfully</strong>
        </div>
    </form>
</div>
12
  • 4
    please dont tag angular questions with angularjs, or with extra non-relevant tags ie: angular4-httpclient Commented May 24, 2018 at 10:53
  • what is loading ? Commented May 24, 2018 at 10:55
  • That's a Boolean i have called in addDetails function, when its true it shows success msg. Commented May 24, 2018 at 10:57
  • formControlName="Name" should be formControlName="name", no? Commented May 24, 2018 at 11:02
  • 2
    Can you reproduce the error here in this stackblitz, I don't see any issues Commented May 24, 2018 at 11:04

2 Answers 2

2

Why you are using no validate in form tag? if no validate is mention in the form ,then the form will not get validated.it will simply execute accept whatever you enter inside the form.try and execute without novalidate ang ngmodel in input tag.it will work

Sign up to request clarification or add additional context in comments.

1 Comment

novalidate is from preventing the browser side default validation.
0

Try this:

<button type="submit" [disabled]="form.status === 'INVALID' || loading"
        class="btn btn-success">Submit</button>

That should consider both options, loading and the invalid form.

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.