I need to listen if there any changes in the form, make enable/disable SAVE button if there changes or not | Angular

Viewed 746

Have issue with listening any changes on a page there is some information, and even if no changes SAVE button active for click

ngOnInit(): void {
        this.createConfigForm()

        this.configFilterForm.valueChanges.pipe(takeUntil(this.unsubscribeAll$)).subscribe(value => {
            value
        })
 }
<!-- ADD BUTTON -->
            <button
                *ngIf="isUpdate()"
                [disabled]="configFilterForm"
                mat-raised-button
                class="add-config-button"
                (click)="onSave()"
                trackClickSnowplow
                id="config-form_save_button"
            >
                <span>SAVE</span>
            </button>

3 Answers

Listen to the form changes like you are doing now, and every time there is a change, enable the button. Disable the button when you click the button, it will stay disabled until change happens. Something like:

configFilterForm: FormGroup;
isDisabled = true;

constructor(private fb: FormBuilder) {
  this.configFilterForm = this.fb.group({
    input1: ['hello']
  });

  this.configFilterForm.valueChanges.pipe(
    takeUntil(this.unsubscribeAll$)
  ).subscribe(value => {
     this.isDisabled = false;
  });
}

And button:

<button [disabled]="isDisabled">Submit</button>

HERE'S A DEMO

BTW, I see you are calling some function *ngIf="isUpdate()" on your button. Please don't do that, they are called on each change detection and can really slow down your application. Use variables instead!

In your HTML file;

<form [formGroup]="formAngular" (ngSubmit)="onSubmit()">
        <label>Name</label>
        <input type="text" formControlName="name">

        {{formAngular.controls.name.errors | json}}

        <p 
            *ngIf="formAngular.controls.name.errors?.required && formAngular.controls.name.touched && formSend">
            It is required your name
        </p>

    <input type="submit" value="Send"  />

    <p  *ngIf="formSend && !formAngular.valid">Form is not complete</p>

</form>

In your component TS file;

  formAngular: FormGroup;

  formSend: boolean;

  constructor() {
    this.formSend = false;
    this.formAngular = new FormGroup({
      name: new FormControl('', [
        Validators.required,
        Validators.maxLength(10)
      ])
  }

onSubmit() {
    this.formSend = true;
    console.log("Form value: ", this.formAngular.value);
  }


ngOnInit() {
    const controlName = this.formAngular.controls.name;
    controlName.valueChanges.pipe(debounceTime(500)).subscribe(value => {
      console.log("Your changes: ", value);
    });
  }

All you need to add is form property pristine or dirty and will detect if you have changed something on the form:

[disabled]="configFilterForm.pristine"

<button
   *ngIf="isUpdate()"
   [disabled]="configFilterForm.pristine"
   mat-raised-button
   class="add-config-button"
   (click)="onSave()"
   trackClickSnowplow
   id="config-form_save_button"
   >
    <span>SAVE</span>
   </button>
Related