Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to user updateOn blur in FormBuilder

I have a custom async validator, and I want to set updateOn: 'blur' property using FormBuilder:

myForm = this.formBuilder.group({
   email: ['', [Validators.required], [this.myAsyncValidator]]
   // ...
});

I tried this but it does not work:

email: ['', [Validators.required], [this.myAsyncValidator, {updateOn: 'blur'}]]

Note

I DO NOT want to create form control instances manually like the following:

myForm = new FormGroup({
    email: new FormControl('', {asyncValidators: [this.myAsyncValidator]}, updateOn: 'blur')
});
like image 772
A-Sharabiani Avatar asked Feb 26 '20 22:02

A-Sharabiani


People also ask

How to blur or update The fullname of a formcontrol?

We use updateOn: 'submit' on the FormGroup level and updateOn: 'blur' on the fullName FormControl. With this, the fullName control will update only when the corresponding input loses focus. For the email control, the updates only happen when the parent form is submitted. This is equivalent to the following code:

How do I blur the value of a form control subject?

So, the first one in our backpack is the updateOn: 'blur'option. Applying this will make the value of a Form Controlsubject to change with the blurevent of a native input element associated with it. Take the following code example,

What is the'updateon'option in the formbuilder API?

For example, if the update strategy of FormGroup or FormArray is set to 'blur', and its children use the 'change' update strategy, the FormGroup or FormArray will still update on 'change' with its children. The updateOn option is also available when using the FormBuilder API to create our form. Below is an example of such usage:

How to set the updateon option after the form has been created?

Angular doesn't provide a way to set the updateOn option after the form controls has been created. With reactive forms, the only way to set the updateOn option is in the constructor of the FormControl, FormGroup, or FormArray classes.


2 Answers

I want to add more answer for this question.

Here is the code at the library:

enter image description here

So, from your question, look like you are trying to set updateOn for email formControl and you are setting 3 arguments. This is not correctly.

Change

myForm = this.formBuilder.group({
   email: ['', [Validators.required], [this.myAsyncValidator]]
   // ...
});

to

myForm = this.formBuilder.group({
   email: ['', {validators: Validators.required, asyncValidators: this.myAsyncValidator, updateOn: 'blur'}]
   // ...
});
like image 95
Tran Son Hoang Avatar answered Nov 11 '22 18:11

Tran Son Hoang


there are two ways to achieve that -

myForm = this.formBuilder.group({email: this.formBuilder.control('', {updateOn: 'blur', validators: [], asyncValidators: []})})

or,

myForm = this.formBuilder.group({email: ['', {updateOn: 'blur', validators:[], asyncValidators: []}]})
like image 29
Nawaz Sharif Avatar answered Nov 11 '22 20:11

Nawaz Sharif