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')
});
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:
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,
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:
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.
I want to add more answer for this question.
Here is the code at the library:
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'}]
// ...
});
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: []}]})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With