Validation error occurs after submitting and resetting an Angular 6 form

In my Angular 6 app, I have a form with input fields and a button. When the button is clicked, the form data is displayed above the form and form.reset() is called to reset the form. However, after the form reset, the input fields turn red due to them being set as required in the form. What could be causing this issue?

app.html

<form [formGroup]='fuelForm'>
      <mat-form-field class="input input2">
        <input matInput placeholder="Nozzle name" formControlName="nozzleName">
      </mat-form-field>
      <mat-form-field class="input input2">
        <input matInput type="number" min="1" id="nozzleNumber" formControlName="nozzleNumber" placeholder="Nozzle number"  >
      </mat-form-field>
      <mat-form-field class="input input4">
        <mat-select placeholder="Fuel type" formControlName="fuelType">
          <mat-option *ngFor="let item of fuelList" [value]="item">{{item}}</mat-option>
        </mat-select>
      </mat-form-field>
      <button mat-icon-button class="circle_button" (click)="add()">
        <mat-icon class="plus_icon">add_circle_outline</mat-icon>
      </button>
    </form>

app.ts

export class DialogNozzleComponent {

fuelForm :FormGroup;

  fuelList = ['Petrol', 'Super petrol', 'Euro4 petrol', 'Gasoline', 'Euro4 gasoline'];
  nozzleItem = [
    {
      nozzleName: 'Nozzle',
      nozzleNumber: '1',
      fuelType: 'Super petrol'
    },
    {
      nozzleName: 'Nozzle',
      nozzleNumber: '2',
      fuelType: 'Gasoline'
    }
  ];

  constructor(public fb : FormBuilder) { 
    this.fuelForm = fb.group({
      nozzleName: {value:'Nozzle', disabled: true},
      nozzleNumber: [null, Validators.required],
      fuelType: [null, Validators.required]
    });
  }

  add() {
    const formValue = this.fuelForm.value;
    formValue.nozzleName = this.fuelForm.controls['nozzleName'].value;
    this.nozzleItem.push(formValue);
    this.fuelForm.controls['nozzleNumber'].reset();
    this.fuelForm.controls['fuelType'].reset();
  }
}

Answer №1

Did you give this a shot?

this.carForm.reset();
this.carForm.markAsPristine();

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

JS click event listener is failing to activate the function as intended

In an attempt to create a modal window, I am facing issues with adding a click event listener to a div that should modify the CSS style of another element. The goal is to change the display property from 'none' to 'border-box' when the ...

Creating a TypeScript type that supports a flexible number of generic parameters

I am currently working on creating an emit function that has the capability to accept multiple arguments. In addition, TypeScript will validate the 2nd argument and beyond based on the 1st argument (the event). The code provided below is a starting point, ...

When trying to parse a non-alphanumeric string using Number.parseInt, the resulting type will be 'number'

Here's what is going on: The typeof Number.parseInt('processed') statement returns 'number'. https://i.sstatic.net/1mpaI.png However, when Number.parseInt('processed') results in NaN. https://i.sstatic.net/gVVgV.png ...

The static files are being received by Django but are not functioning properly

I've been working on a project using django 1.7, and below is my settings.py configuration: STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "assets"), ) STATIC_ROOT = "absolute/path/to/static" In the 'assets&apo ...

Does anyone know if it's feasible to return a value from PHP to an HTML form without relying on JavaScript?

Currently, I am in the process of learning how to create a basic web form. My goal is to develop an HTML web form that sends a number to PHP and then displays the number in another text field without refreshing the page. After doing some research online, ...

Issue with Jquery Drag and Drop functionality, navigate to a different page

I am trying to incorporate a page with js from quotemedia.com using Jquery. When I insert the js into the sortable, and then drag and drop the element containing the js, it suddenly switches to full page display. This issue occurs in Firefox, but IE works ...

The mysterious appearance of the <v-*> custom element in Vuetify Jest

Currently, I am in the process of writing unit tests for my project using Jest. The project itself is built on Vue, Vuetify (1.5), TypeScript, and vue-property-decorator. One particular area of focus for me has been creating a basic wrapper for the <v- ...

Having issues with Bootstrap 4 on mobile - site stretches too wide and causes side-scrolling. Is it a possible syntax error?

Currently, I am working on a website that utilizes multiple backgrounds layered on top of each other using Sass and Bootstrap. However, I am encountering a problem in the mobile view where users can scroll horizontally, which is not desired. Additionally, ...

Accidentally released a lone character initiator instead of the intended character

There seems to be an issue where \u009a is being received in xmlhttp.responseText instead of \u0161, and the reason behind this is unclear. The desired š character should display in the textbox, but only the single character introducer is showin ...

Mongoose Express: Limiting increments to a maximum of 5

Currently, the essential functionality implemented is 1 click = 1 vote. The system successfully updates a vote parameter in MongoDB and increments it as expected. However, after approximately 5 votes, the incrementing process halts. Upon refreshing the bro ...

using async.waterfall with async.apply

Here is a code snippet that I am working with: async.waterfall([ // Read directory async.apply(fs.readdir, '../testdata'), // Load data from each file function(files, callback) { async.each(files, loadDataFromFile, callback); } ], ...

How can I subtract a value from my array in Angular?

I've been troubleshooting this problem for a while now and I'm hoping that someone here can assist me with finding a solution. The issue at hand involves an array object containing various values such as id, title, amountCounter. Specifically, t ...

Guide to incorporating the useEffect hook within a React Native View

I am trying to use useEffect within a return statement (inside a Text element nested inside multiple View elements), and my understanding is that I need to use "{...}" syntax to indicate that the code written is actual JavaScript. However, when I implement ...

PHP unable to display HTML form element using its designated ID attribute

I've been experiencing some difficulties with PHP echoing a label element that contains an ID attribute within an HTML form. My intention was to utilize the ID attribute in order to avoid having to modify the JS code to use the name attribute instead. ...

Having difficulty setting a value for a tooltip with replaceWith() function

When using jQuery's .replaceWith() method to insert new DOM contents, I noticed that all content gets replaced except for the value of the title. Even though I tried different approaches, the tooltip only displays {{descriptions.title}} instead of the ...

Executing JavaScript code in the Selenium IDE

I'm having trouble figuring out how to execute JavaScript within the Selenium IDE. The objective is to enter text into an input field, with a backend setup that verifies the current time in the input field for testing purposes: Here's the input f ...

"Uploading New Content: How to Rotate Text and Images on a Website with

My static website is currently live and running smoothly. However, I am looking for a way to update certain text and images dynamically, without having to delve into the code each time. Can anyone recommend a simple solution? ...

What is the best way to display a list of items in Vue JS while maintaining the

Looking to display my Vue.js list in reverse order using v-for without altering the original object. The default HTML list displays from top to bottom, but I want to append items from bottom to top on the DOM. Telegram web messages list achieves this wit ...

Using Node-twitter within the Express framework

I'm currently exploring the node-twitter module within the express framework. Although I have a basic understanding of node, including routing, rendering jade templates, and installing packages, I find myself overwhelmed by the sheer number of package ...

After completing the purchase, direct the user back to the HomePage

Currently, my development stack involves Reactjs for the UI and Java with Spring Boot for the backend. I have a specific question regarding user redirection after a purchase. For example, how can I direct the user back to the HomePage if they click the b ...