1

I'm about to realize the validation of a pretty massive form. In some cases I have to compare (conditions) different input values to validate.

Following example given:

function frmDealValidation () {
   var validator = $("#frmDeal").validate({
      rules: {
        product_value: {
             required: true,
             number: true,
             min: 0.01,
         }
         price: {
             required: true,
             number: true,
             min: 0.01,
             max: ($("#show_value1").is(':checked')) ? $("#product_value").val() : 1000000
         }
      },
      submitHandler: function(form) {
          form.submit();
      }
  });
}

As you can see above the max-check depends on a checked radio button and returns a value. That means that the price is not allowed to be greater than the actual product_value.

Assuming the filled in price is greater than the product_value the validation works fine after the first form submit.

Now I can change the values in the inputs to correct the mistakes or if I'm stupid I fill them again with not valid values and hit the submit button (calls frmDealValidation ()).

Unfortunately the validation didn't update the values from $("#product_value").val() and seems to use the cached value from the first submit.

I tried validator.resetForm(); as I thought it will force validation to re-validate the whole form and with updated values but with no success!

May I ask for HELP?

Cheers,

Norbert

KiwiJuicer
  • 1,952
  • 14
  • 28

3 Answers3

2

I would go this way:

$.validator.methods.priceMaxValidator = function(value, element, param) {
    return ($("#show_value1").is(':checked')) 
        ? $("#product_value").val() : param.max
}

and then in validator setup:

price: {
    required: true,
    number: true,
    min: 0.01,
    priceMaxValidator: { max: 100000 }
}

This way validation plugin will call priceMaxValidator on each price validation and it will evaluate validation expression every time. You don't need resetForm().

nrodic
  • 3,026
  • 3
  • 33
  • 38
1

Thanks nrodic,

you gave me the hints i needed:) As I didn't get it work with your suggestion I went kinda similar way to get it work.

Here is the code I use now, it's using addMethod():

$.validator.addMethod("checkIfLessThanValue", function(value, element, params) {
    if(params) {
        return this.optional(element) || (($("#show_value1").is(':checked') && ($("#price").val() > $("#deal_value").val()))) ? false : true;
    } else {
        return true;
    }
}, $.format("The Price is greater than Deal Value"));

And here is the rule I'm using the Method:

price: {
    required: true,
    number: true,
    min: 0.01,
    checkIfLessThanValue: true
}

Thanks for your help!!

Norbert

KiwiJuicer
  • 1,952
  • 14
  • 28
  • You are welcome. I think `addMethod()` is preferred way, so you might want to accept your own answer, just for closing the case. :) – nrodic Nov 14 '12 at 22:47
0

I'm sorry guys, I just found a bug in my previous solution, just want you to know about:

We want to compare to float values but we compare just STRINGS at the moment, here is the fix: I needed to use the parseFloat():

$.validator.addMethod("checkIfLessThanValue", function(value, element, params) {
    if(params) {
        return this.optional(element) || (($("#show_value1").is(':checked') && (parseFloat($("#price").val()) > parseFloat($("#deal_value").val())))) ? false : true;
    } else {
        return true;
    }
}, $.format("The Price is greater than Deal Value"));

Cheers

KiwiJuicer
  • 1,952
  • 14
  • 28