I have a simple reactive form like this:
initSignupForm() {
return new FormGroup({
firstName: new FormControl(this.formData.firstName, [
Validators.required,
]),
lastName: new FormControl(this.formData.lastName, [Validators.required]),
userName: new FormControl(this.formData.userName, {
validators: [Validators.required],
asyncValidators: [
this.authService.checkUserName.bind(this.authService),
],
updateOn: 'submit',
}),
userEmail: new FormControl(this.formData.userEmail, {
validators: [Validators.required, Validators.email],
asyncValidators: [
this.authService.checkUserEmail.bind(this.authService),
],
updateOn: 'submit',
}),
});
}
For the userName
and userEmail
I have an async validator (an api call to the backend). It works all fine.
For submitting the form I have the usual lines in the template:
<form [formGroup]="signupForm" (ngSubmit)="submitForm()">
and a submit button:
<button
type="submit"
kendoButton
themeColor="primary"
fillMode="solid"
size="large"
>
Regisztráció
</button>
The submitForm
event looks like this:
submitForm() {
this.signupForm.markAllAsTouched();
if (this.signupForm.valid) {
const user = this.signupForm.value;
console.log(user);
}
}
The issue is, that after filling in the form (all fields), I hit enter, the async validators run and pass and I have to press enter one more time in order to get the form valid and submitting. This is because of the updateOn: 'submit'
strategy.
After submitting, the control values get populated and the form gets valid. And only after that can the form really submitted and the data sent to the backend. Basically I have to click on the submit button twice.
How can I overcome this, but keep the submit strategy?
This must be related to the async validator, because if I remove this, I have to submit the form only once.