Validation not correclty throwing messages

I am trying to validate some amount input for saving some money. First, I declared some cases/reasons and max amounts:

Reason: Rent (maxAmount) = 100€;
Gaming = 50€;
Electronic: 200€;
Food: 0€;

The amount I want to enter can't be greater than the maxAmount of the reason. If the maxAmount is 0, there is no limit, but the number.length shouldn't be greater than 5.

:rules="[ v=> (!!v ||$t('goodwillMain.goodwillAmountRequired')),
          v=> (!v || !!v && spending.amount > 0)|| $t('Amount must be greater than 0'),
          v=> (!v || !!v && reason.maxAmount > 0 && spending.amount.toString().length <= 6)
              ||('max Number length = 5'),
          v=> (!v || !!v && reason.maxAmount <= 0 && spending.amount <= reason.maxAmount)
              || ('Your spending amount must be smaller than the maximumAmount ')]"

For example, if I select the reason "food" (maxAmount=0, no limit, but number.toString().length<=6), I want to enter 1000, but I'm getting the validation message from my third rule ("Your spending amount must be smaller than the maximumAmount"). The third rule should only be checked, if the reason.amount is greater than 0.

1 answer

  • answered 2021-04-15 03:44 tony19

    The last rule doesn't seem to make sense:

    v=> (!v || !!v && reason.maxAmount <= 0 && spending.amount <= reason.maxAmount)
                      ^^^^^^^^^^^^^^^^^^^^^
        || ('Your spending amount must be smaller than the maximumAmount ')]
    

    The clause reason.maxAmount <= 0 would allow negative max amounts. That should probably be reason.maxAmount > 0.