0

I have tried to disable all html elements in a form in Angular 16. But it is not working as expected. But in the Google Login Form all fields including <a> tag are in disabled state.

enter image description here

This my login.component.html

<div class="signin-wrapper">
  <mat-card>
    <form [formGroup]="loginForm" (ngSubmit)="onFormSubmit()">
      <mat-card-header>
        <mat-card-title>Sign in</mat-card-title>
      </mat-card-header>
      <mat-card-content>
        <mat-form-field appearance="outline">
          <mat-label>Email</mat-label>
          <input required matInput type="email" placeholder="eg. test@email.com" formControlName="userEmailAddress">
        </mat-form-field>
        <mat-form-field appearance="outline">
          <mat-label>Password</mat-label>
          <input required matInput type="password" placeholder="eg. ************" formControlName="userPassword">
        </mat-form-field>
      </mat-card-content>
      <mat-card-actions>
        <button mat-raised-button color="primary" type="submit">Sign in</button>
      </mat-card-actions>
      <mat-card-footer>
        <div class="text-center">Not Registered? <a routerLink="../register">Register</a></div>
      </mat-card-footer>
    </form>
    <mat-progress-bar *ngIf="inProgress" mode="indeterminate"></mat-progress-bar>
  </mat-card>
</div>

I am using

this.loginForm.disable()

to disable the form. The result is

enter image description here

In the above form <a> tag and <button> are not disabled.

Then I tried with <fieldset [disabled]="true">

<div class="signin-wrapper">
  <mat-card>
    <form [formGroup]="loginForm" (ngSubmit)="onFormSubmit()">
      <fieldset [disabled]="true">
      <mat-card-header>
        <mat-card-title>SIgn in</mat-card-title>
      </mat-card-header>
      <mat-card-content>
        <mat-form-field appearance="outline">
          <mat-label>Email</mat-label>
          <input required matInput type="email" placeholder="eg. test@email.com" formControlName="userEmailAddress">
        </mat-form-field>
        <mat-form-field appearance="outline">
          <mat-label>Password</mat-label>
          <input required matInput type="password" placeholder="eg. ************" formControlName="userPassword">
        </mat-form-field>
      </mat-card-content>
      <mat-card-actions>
        <button mat-raised-button color="primary" type="submit">Sign in</button>
      </mat-card-actions>
      <mat-card-footer>
        <div class="text-center">Not Registered? <a routerLink="../register">Register</a></div>
      </mat-card-footer>
      </fieldset>
    </form>
    <mat-progress-bar *ngIf="inProgress" mode="indeterminate"></mat-progress-bar>
  </mat-card>
</div>

The result is

enter image description here

Still <a> tag is in enabled condition.

Is it possible to do like Google?

Pamba
  • 776
  • 1
  • 16
  • 29

2 Answers2

1

I have implemented a logic for this. But I don't know is it the best method.

login.component.css

.wrapper-disable{
  z-index: -1 !important;
  position: relative;
  opacity: 0.5;
  pointer-events:none;
  -webkit-user-select: none; /* Safari */
  -ms-user-select: none; /* IE 10 and IE 11 */
  user-select: none; /* Standard syntax */
}
.wrapper-disable *{
  pointer-events:none;
  -webkit-user-select: none; /* Safari */
  -ms-user-select: none; /* IE 10 and IE 11 */
  user-select: none; /* Standard syntax */
}

login.component.html

<div [ngClass]="{'signin-wrapper':true, 'wrapper-disable' : wrapperDisable}">
  <mat-card >
    <form [formGroup]="loginForm" (ngSubmit)="onFormSubmit()">
      <mat-card-header>
        <mat-card-title>SIgn in</mat-card-title>
      </mat-card-header>
      <!-- <mat-divider></mat-divider> -->
      <mat-card-content>
        <mat-form-field appearance="outline">
          <mat-label>Email</mat-label>
          <input required matInput type="email" placeholder="eg. test@email.com" formControlName="userEmailAddress">
        </mat-form-field>
        <mat-form-field appearance="outline">
          <mat-label>Password</mat-label>
          <input required matInput type="password" placeholder="eg. ************" formControlName="userPassword">
        </mat-form-field>
      </mat-card-content>
      <mat-card-actions>
        <button mat-raised-button color="primary" type="submit">Sign in</button>
      </mat-card-actions>
      <mat-card-footer>
        <div class="text-center">Not Registered? <a routerLink="../register">Register</a></div>
      </mat-card-footer>
    </form>
    <mat-progress-bar *ngIf="inProgress" mode="indeterminate"></mat-progress-bar>
  </mat-card>
</div>

login.component.ts

onFormSubmit(): void {

    if (this.loginForm.valid) {

      this.wrapperDisable = true;
      // 
}

output is:

enter image description here

It is working as expected!!!

Pamba
  • 776
  • 1
  • 16
  • 29
0

Have you tried [disabled]=true on the <form></form> element?