[jQuery] [validate] Resetting validation rules dynamically
I'm currently using the jQuery Validation plugin on a page that needs
to dynamically update the particular rules you validate against
according to which options the user selects (imagine a payment page
which accepts different payment methods; you wouldn't want to validate
against credit card fields if the user isn't paying by credit card).
I've been working under the assumption that I could do this simply by
firing the $().validate(options); method again, with a new set of
arguments, but this doesn't appear to be working, and the original
rules are still being applied (leading to some annoying interactions,
as the user who said they didn't want to pay by credit card is being
told their credit card number is missing).
Essentially, within my code I have the equivalent of the following:
$('#paymentMethod input[type=radio]').change(function(){
$('#paymentMethod input[type=radio]').each(function(){
if (this.checked) {
switch ($(this).val())
{
case 'pay_cc':
$('#myForm').validate(rulesForCC);
break;
case 'pay_offline':
$('#myForm').validate(rulesForOffline);
break;
}
}
}
});
(forgive any slight errors in the above; I've redacted a large part of
it, so I may have introduced bugs).
Is there any mechanism for clearing existing rules on a form, or in
any other way allowing me to reset the rules more explicitly?