1

この記事のように、Javascript で jQuery Validate ルールを設定しています 。 -2

ルールを validationRules という jsony オブジェクトに保存し、Validate プラグインを呼び出すときにルールを渡します。

$("#myForm").validate(validationRules);

後で、次のように、ユーザーの操作に基づいてプログラムで値を変更したいと思います。

validationRules.rules.Quantity.min=10;

値が更新されても、Validate は新しい値を受け入れません。また、プラグインを再呼び出ししてみました:

$("#myForm").validate(validationRules);

しかし、それは役に立ちませんでした。

プラグインと直接話すことができます:

$("#Quantity").rules("add", {min: 10});

そして、期待どおりに動作します。そのため、その構文を使用してオンザフライでルールを変更しますが、validateRules で更新されたルールを尊重するように Validate に指示する方法について、誰かが光を当てることができますか?

4

1 に答える 1

2

I had the same problem and I have spent a lot of time trying to solve it. Finally I find a trick in the Validation Plugin creator´s GitHub account:

var $form       = $('#formId'),
    newRules    = {rules...},
    newMessages = {messages...};

$.each(newRules , function(key, value) {
    newRules [key] = $.validator.normalizeRule(value);
});
$.extend( $form.validate().settings.rules, newRules );
$.extend( $form.validate().settings.messages, newMessages );

I am not quite sure about the messages, I am using it right now and seems to work fine. This is the link where you can read more about it: https://github.com/jzaefferer/jquery-validation/issues/214

于 2012-09-13T23:46:21.867 に答える