Help with RxwebValidators.custom and RxwebValidators.customAsync
Hello, I need help with the RxwebValidators.custom and RxwebValidators.customAsync method in Angular
I’m doing some tests to understand how the library works.
I created my form with three attributes:
this.entidadeForm = this.formBuilder.group({
age: [0, [RxwebValidators.required({ message: 'Required field' }), RxwebValidators.minNumber({ value: 10, message: 'Minimum of 10' })]],
multiple: [0],
beast: [0, []]
});
For my test I need the beast field to be equal to age * multiple
this.entidadeForm.get('beast')?.addValidators(RxwebValidators.custom({
customRules: [(formGroupValue: any, parentObject: any, additionalValue: any) => {
if ((formGroupValue.idade * formGroupValue.multiplo) === formGroupValue.besta) {
return null;
} else {
return { 'custom': { isValid: false, message: 'The beast value is not valid' } };
}
}
],
additionalValue: null
}))
However, when modifying the input to beast, its value is read within my condition, always a previous state.
If I enter 1, it remains at 0, which is its initial state. If I type 10, it changes to 1. If I type 100, it changes to 10. And so on.
Why does it happen? Is it a library error or am I not handling my object correctly?
I thought that in this case I should work with asynchronicity, but when changing to customAsync
this.entidadeForm.get(‘beast’)?.addAsyncValidators(RxwebValidators.customAsync(…)
I get the error APP_VALIDATORS[validatorName] is not a function
Input age: 10; multiple: 10; beast: 100 –> The beast field returns the error “The beast value is not valid”, but it should not return any error.
user24903039 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.