The Angular NgForm method form.resetForm does not seem to be resetting properly when dealing with arrays

I encountered an issue with my simple ngForm that was previously functioning well with string input and single select dropdowns. When I added a multi-select dropdown that introduces an array, I started facing a strange problem. Even after using form.formReset() to save or reset the form, I still receive a "field required validation" error message, which is only resolved with a refresh.

The validation problem arose after adding the multi-select dropdown. I have tried several approaches to solve it, but without success. The code snippet is provided below:

<form #form="ngForm"
      class="form-horizontal"
      (ngSubmit)="save(form)">
  <inf-modal #modal
             (onHide)="form.resetForm()"
             size="medium">
    <mat-card>
      <mat-card-title class="inf-modal-title">
        Modal View
        <button type="button"
                class="close"
                aria-label="Close"
                (click)="modal.hide()">
          <span aria-hidden="true">
            <mat-icon>clear</mat-icon>
          </span>
        </button>
      </mat-card-title>
      <mat-card-content>
        <div class="row">
          <div class="col-lg-12">
            <div class="form-group">
              <div class="row">
                <div class="col-lg-8">
                  <inf-person-org-composite-entity-selector name="autocompleteSelector"
                                                            [(ngModel)]="selectedTarget"
                                                            (ngModelChange)="storeTargetId($event)"
                                                            [blackList]="blacklistedTargets"
                                                            [multiSelect]="false"
                                                            [required]="true"
                                                            [placeholder]="'Name"

                                                            [entitySelectorVisible]="false">
                  </inf-person-org-composite-entity-selector>
                </div>

                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="row"
             *ngIf="!isTargetInvestigation()">
          <div class="col-lg-6">
            <inf-multiselect #linkType="ngModel"
                        name="linkType"
                        [(ngModel)]="record.linkType"
                        [optionKey]="'linkType'"
                        [placeholder]="'linkType' "
                        [required]="true"></inf-multiselect>
          </div>
        </div>
        <div class="row"
             *ngIf="!isTargetInvestigation()">
          <div class="col-lg-12">
            <div class="form-group">
              <mat-form-field>
                <input matInput
                       #comment="ngModel"
                       [(ngModel)]="record.comment"
                       name="comment"
                       [placeholder]="'comment' ">
              </mat-form-field>
            </div>
          </div>
        </div>
      </mat-card-content>
      <mat-card-actions>
        <button type="button"
                (click)="modal.hide()"
                mat-button
                color="primary">{{ 'common.cancel' | translate }}
        </button>
        <button type="reset"
                (click)="form.resetForm()"
                mat-button
                color="primary">{{ 'common.reset' | translate }}
        </button>
        <button mat-raised-button
                color="primary"
                type="submit">{{ 'common.assign' | translate}}
        </button>
      </mat-card-actions>
    </mat-card>
  </inf-modal>
</form>

I am unsure of what I am missing; I have even experimented with the pristine and dirty properties of ngForm.

https://i.sstatic.net/qX7Kx.png

Answer №1

After carefully reviewing various discussions and forums, my recommendation would be to consider updating the multi-select component. Perhaps implement a reset function within the component or create a new one, and include .markAsPristine() and .markAsUntouched() methods to check for conditions whenever a reset is triggered, such as when a blank value is selected.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmitting form data inputted by the user to a modal that resides in the same component, all without the need for child or parent components or

In need of a solution where users can input answers to questions and have all the entered data displayed in a popup alongside the respective question. If a user chooses not to answer a question, I do not want that question or any related information to be ...

Ways to resolve Cross-Origin Resource Sharing (CORS) issue encountered in Report

I am currently working on an Angular project and trying to render SSRS reports within the app by utilizing a specific package. The application is hosted at http://localhost:52698/ while the SSRS server resides on a different domain http:\ssrsserv ...

Sending data to back-end server using Multer, Node.js, and Express via a POST request

I have been facing an issue while trying to send a form from my local system to the backend server via a POST request. Every time, it seems to go with --binary-data instead of going with --form. Below is a sample curl command that I am looking to replicat ...

When attempting to retrieve data from an API in Angular 8, I encountered difficulties in dynamically creating a formArray within a formArray

I am struggling to dynamically create form controls based on the data received, specifically for fields like task and template name. Your assistance is greatly appreciated. { "items": [ { "templatename": "Defult" ...

When set to synchronize:true in Typeorm, any changes to an entity will result in the many-to

As a newcomer to typeorm, I've encountered a peculiar issue with the synchronize: true setting in my ormconfig.js. Whenever I make changes to an entity with a Many-to-Many relationship, any data present in the join table prior to the entity modificati ...

How can you include a multi-layered array within another multi-layered array using TypeScript?

If we want to extend a two-dimensional array without creating a new one, the following approach can be taken: let array:number[][] = [ [5, 6], ]; We also have two other two-dimensional arrays named a1 and a2: let a1:number[][] = [[1, 2], [3, 4]]; let ...

Resizing a d3 graph for various screen resolutions

Looking to create a dynamic dashboard in Angular, I found a code snippet for a gauge chart at this link. Here is an example of how my HTML file appears: <div fxLayout="row" fxLayoutAlign="space-around center" > <div fxFlex='33%'> ...

Encountering issues with MatTable functionality in Angular version 10

Could it be that I’m starting this project from scratch using Angular Material 10, a framework I’m not familiar with yet, or am I simply missing something? My mat-table isn’t showing up on the page at all, which is completely new to me. Here’s the ...

Retrieve select options only upon clicking

Essentially, I have a default value that needs to be loaded into a select element and then make an API call to load the rest only when the select is clicked. <select (click)="getSubevents(market_uri)"> <option *ngFor="let subeven ...

Angular directive problem

Within the module, I have defined a directive but the <div> is not being highlighted as expected. test.directive.ts import { Directive, ElementRef, HostListener, Input } from "@angular/core"; @Directive({ selector: '[test]' }) expor ...

"What could be the reason for my ngFor not working properly with an asynchronous

<div class="panel-body"> {{ (currentPlatformProfile | async)?.length }} <!-- line 1 --> <table> <tr *ngFor="let platform of {{(currentPlatformProfile | async)}}"> <td>{{platf ...

When logging off from an Angular2 application, the OIDC-client does not properly clear the cookies for the MVC application

I currently have an authorization server that is being used by both my Angular2 app and MVC webapp. In my Angular2 app, I've implemented authorization using the oidc-client JavaScript package. Everything works well except for the logout functionality ...

Creating a personalized button in DraftJs for inserting customized HTML with CSS styles

When utilizing DraftJs with WYSWYG, my task involves creating a custom button in the toolbar. Upon clicking this button, I intend to insert custom HTML (containing <button>) directly into the content. While I am familiar with adding a custom button ...

Webpack 4.1.1 -> The configuration.module contains a property 'loaders' that is unrecognized

After updating my webpack to version 4.1.1, I encountered an error when trying to run it: The configuration object is invalid. Webpack has been initialized with a configuration that does not match the API schema. - The 'loaders' property in ...

Tips for presenting SVG symbols using Interpolation within Angular 7 from a JSON document

When it comes to displaying content in Angular 7 components, JSON is used. However, I have encountered a problem while trying to incorporate SVG icons from our UX team into the component using JSON. Using the img tag restricts me from applying a CSS class ...

Exploring the possibilities of TypeScript/angularJS in HTTP GET requests

I am a beginner in typescript and angular.js, and I am facing difficulties with an http get request. I rely on DefinitelyTyped for angular's type definitions. This is what my controller code looks like: module game.Controller { 'use strict& ...

What is the correct way to invoke a function that accepts a combination of specific string values when all I have is a regular string?

Within the TypeScript function declaration provided below, the parameter type alignment consists of unioned literals. function printText(s: string, alignment: "left" | "right" | "center") { // ... } As per the documentation ...

I am experiencing slow load times for my Angular 2 app when first-time users access it, and I am seeking assistance in optimizing its speed

Below, you'll find a snippet from my app.ts file. I'm currently working with angular2, firebase, and typescript. I'm curious if the sluggish performance is due to the abundance of routes and injected files? The application functions smoot ...

Tips for executing DOM manipulation within Angular components

What is the best way to access DOM elements in Angular (Version 2.x and above)? Since basic functions such as addClass and removeClass are not available in typescript, how can we perform DOM manipulations in Angular components? Any suggestions would be g ...

Exploring the angular2-meteor Component Testing

For my web app built with angular-meteor, I attempted to write some test functions in order to test the functionality. However, when using the command meteor test --driver-package practicalmeteor:mocha, it does not execute any test files and results in 0 f ...