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?
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.
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.
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.
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,
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With