Consider a form whit this Validation that updates on submit like this:
Html
<div class="form-row">
<form [formGroup]="myForm" class="col-3" (submit)="onSubmit()">
<label for="name">Name</label>
<input class="form-control" formControlName="name" id="name" name="name" />
<button class="button" type="submit">Save</button>
</form>
Value: {{ myForm.value.name }}
<br />
Is form valid {{ myForm.valid }}
</div>
And in the ts
file:
ngOnInit(): void {
this.myForm = this.fb.group(
{
name: ['', [Validators.required, Validators.pattern(/^[^\d].*/)]],
},
{ updateOn: 'submit' }
);
}
onSubmit() {
if (this.myForm.invalid) {
return;
} else {
console.log('Form is Valid');
}
}
I've putted the if
condition to check if the form is not valid and just return. To achieve that if the form is invalid the input is not allowed to change value, but it's not working. Even when I typed something that is not allowed for the input and hit the submit button the form becomes invalid but values also gets updated.
Tried also with onUpdate
blur.