10

Is it possible to disable the whole form (group) in angular instead of doing it for every input separately?

Something like <input [disabled]="formNotValid"/> but for a <form> or a <div ngModelGroup..>?

3 Answers 3

13

If You use ReactiveForms just write

form: formGroup;

this.form.disable();

In case of ngForm you can write like this' created plunker

 <form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
  <input name="first" ngModel required #first="ngModel">
  <input name="last" ngModel>
  <button>Submit</button>
</form>

<button (click)="disable(f)">Disable form</button>



 disable(f) {
    f.form.disable()
  }
Sign up to request clarification or add additional context in comments.

1 Comment

I use template driven forms in this case.
2

Here is a solution that automatically disables all form-elements when the submit-button is clicked.

This solution applies to template-driven forms.

HTML:

<form #myForm="ngForm" (ngSubmit)="onFormSubmit( myForm )">

    <input type="text" name="title" [(ngModel)]="model.title" #title="ngModel">

    <button type="submit">

        <span *ngIf="!imyForm.disabled">
            Submit Form
        <span>

        <span *ngIf="myForm.disabled">
            Submitted
        <span>

    </button>

</form>

TS:

public onFormSubmit( form: any): void {
    form.form.disable();
}

Comments

1

It could be considered hack-y, but you could use ngClass and css to apply a class that turns off pointer events for all inputs inside a container when conditions are met.

.disable-inputs {
  pointer-events: none;
}

<form [ngClass]="{'disable-inputs':[true/false condition]}">
   // input elements
</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.