asobirov
asobirov

Reputation: 572

Angular10 Reactive Form validation both on blur and submit

I'm trying to validate the form on blur and every time the submit button is being pressed, however there is no option as:

updateOn: ['blur', 'submit']

It is either updateOn: 'blur' or updateOn: 'submit'

Is there a way or workaround to achieve this?

Upvotes: 1

Views: 501

Answers (1)

BoyFarmer
BoyFarmer

Reputation: 101

There is workaround for you :)

How to validate form on submit

Depending on your form structure you have to iterate on every field and mark it as touched.

For flat from group it should be enough

Object.keys(form).forEach(field => {
  const control = form.get(field); 
  control.markAsTouched({ onlySelf: true });
});

Of course you have execute that code in you onSubmit method

Upvotes: 1

Related Questions