Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6: Reactive Form's updateOn: 'submit' is not updating value after submitting

I'm testing with Angular reactive form validation with updateOn parameter but it seems that even the simplest form is not working correctly when I pass updateOn: 'submit' (other values are working fine).

app.component.html:

<form novalidate (submit)="onSubmit()">
  <input type="text" [formControl]="form.get('test')">

  Value: {{ form.get('test').value }}

  <button type="submit">SUBMIT</button>
</form>

app.component.ts:

import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  public form = new FormGroup({
    test: new FormControl('', { validators: [Validators.required], updateOn: 'submit'}),
  });

  public onSubmit(): void {
    console.log('Submitted:');
    console.log(this.form.value); // Got: Object { test: "" }
  }
}

Full and working code: https://codesandbox.io/s/67pmd

What I'm getting after clicking a button is empty field value (both in html and in console). But if I change/leave updateOn to default, then it's working fine. From what I see, value and validations should be updated only after submitting the form, but it seems that it doesn't? What I am missing here?

like image 786
Gytis TG Avatar asked Aug 11 '19 18:08

Gytis TG


People also ask

What is the updateon option in angular forms?

The updateOn Option in Angular Forms When using Angular Forms, by default, on every keystroke, the values of your form controls are updated and their associated validators are executed. This may not be always desirable. The updateOn option gives us a finer-grained control over the moment when value updates and validators are triggered.

What's new in Angular 5 form validation?

Angular Form Validation on Blur and Submit Angular In Angular 5, two new form validation techniques are introduced. Considering that you are familiar with FormBuilder, FormGroup, FormControl, Validators and etc.

What is the use of updateon submit in form control?

Similarly the updateOn: 'submit'option will make the value/values of the Form Control(s) subjected to change on a submitevent fired on the native form element. Following code shows you how achieve this behavior for all the Form Controls.

What's new in Angular 5?

In Angular 5, two new form validation techniques are introduced. Considering that you are familiar with FormBuilder, FormGroup, FormControl, Validators and etc. I’ll just jump into some examples. To, give you a heads up, here is how our form model looks like,


1 Answers

You seem to have forgotten to bind your formGroup to your template. The form FormGroup is bound to the <form> element with the formGroup directive. (Source: docs)

<form [formGroup]="form" novalidate (submit)="onSubmit()">
    <input type="text" [formControl]="form.get('test')">
    Value: {{ form.get('test').value }}
    <button type="submit">SUBMIT</button>
</form>

Edit: The reason why other values like blur and change are working fine with updateOn is beacause blur and change are events fired based on your input while submit is an event that's fired when the form itself is submitted. To know when the form is submitted, Angular needs to know what the formGroup is.

like image 61
nash11 Avatar answered Nov 18 '22 14:11

nash11