Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greater than Cross field validation in Angular Validator

In my form some fields are depended on other fields. So I want to validate the control when the value changes will occur. I know about the global validator on formgroup. but I don't want to use that due to some other issues.

Scenario : If user enters a value in Car Amount field based on that user cannot enters the greater than value in Premium Amount textbox.

I have applied a custom validator on 'premiumAmount' field, but when I change the value in the 'carAmount' field then premiumAmount validation will not fire.

How to resolve this?

like image 345
Craig Avatar asked Mar 02 '26 23:03

Craig


1 Answers

Instead of making Custom Greater than validation, this can be achieved through greaterThan validator of @rxweb/reactive-form-validators.

You just need to mention in your greatherThan validator in your formGroup. Here is the code:

 ngOnInit() {
        this.userFormGroup = this.formBuilder.group({
            carAmount:['',], 
            premiumAmount:['', RxwebValidators.greaterThan({fieldName:'carAmount' })], 
        });
    }

Here is the forked stackblitz.

like image 58
Srishti Khandelwal Avatar answered Mar 04 '26 22:03

Srishti Khandelwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!