Home > database >  Angular - How to stop trigger all form field validation when update a single input
Angular - How to stop trigger all form field validation when update a single input

Time:04-28

I have a form that contains the following inputs: first name, last name, username and password.

I can generate a random password using a button, using set/patchValue into the control will trigger all validation (for first name, last name and username), but I want to trigger the validation just for my input (password)

this.userDetailsFormGroup
        .get('password')
        ?.patchValue(usersData.data.toString());

How can I do this?

CodePudding user response:

Make sure your buttons have the type="button" attribute on them (except for the Save button, of course), otherwise they will trigger the submit event because the default type of a button is submit. That's why you get the validation errors on all the fields.

Another note: When you update the value like you do (by grabbing the firstName control and setting a primitive value on it), you can get away with using setValue instead of patchValue. The patchValue is useful when you are setting a "complex" value and you only update it partially. For example, you can do something like this:

this.userDetailsFormGroup.patchValue({ firstName: usersData.data.toString() });
  • Related